List Of PHP Array Functions

PHP arrays are a special type of variable that holds multiple values of the same data type at a time.

PHP arrays are divided into three parts:

  1. Indexed array – having a numerical index
  2. Associative array – having named keys
  3. Multidimensional array – contains one or more arrays

PHP Array Functions

As there are three types of arrays in PHP but there is no difference in applying any array function to them.

The array() Function

The array() function is the main method to initialize an array.

Syntax:

array(value1,value2,value3,..etc)

Or

array(key1 => value1,key2 => value2,key3 => value3,..etc)

Example:

$social_media = array("facebook","instagram","whatsapp","Telegram");
$person = array("name"=>"Nick","age"=>18,"eye color"=>"black");

The array_change_key_case() Function

The array_change_key_case() function is used to change array key elements to lower or upper case. There are two parameters to be passed in function which are as below.

  1. CASE_LOWER – It changes key text to lower case
  2. CASE_UPPER – It changes key text to upper case

Syntax:

array_change_key_case(array,case);

Example:

$person = array("name"=>"Nick","age"=>18,"eye color"=>"black");
print_r(array_change_key_case($person,CASE_UPPER));

Output:

Array ( [NAME] => Nick [AGE] => 18 [EYE COLOR] => black )

The array_chunk() Function

The array_chunk() function splits the array into a specific sized array chunks.

Syntax:

array_chunk(array,size);

Example:

$social_media = array("facebook","instagram","whatsapp","Telegram");
print_r(array_chunk($social_media,2));

Output:

Array ( [0] => Array ( [0] => facebook [1] => instagram ) [1] => Array ( [0] => whatsapp [1] => telegram ) )

The array_column() Function

The array_column() function returns the array with values of a specified column key name from the main array.

Syntax:

array_column(array,column_key,index_key);

Note: The column_key is the key element of the array and Index_key is an optional field.

Example:

$person = array(
array("name"=>"Nick","age"=>18,"eye color"=>"black"),
array("name"=>"Monil","age"=>19,"eye color"=>"blue"),
array("name"=>"Yash","age"=>22,"eye color"=>"black")
);
print_r($person,"name");

Output:

Array
(
[0] => Nick
[1] => Monil
[2] => Yash
)

The array_combine() Function

The array_combine() function creates the array having the keys from one array and values from another one.

Syntax:

array_combine(keys,values);

Example

$name=array("Nick","Yash","Monil");
$score=array(21,19,19);
print_r(array_combine($name,$score));

Output:

Array ( [Nick] => 21 [Yash] => 19 [Monil] => 19 )

The array_count_values() Function

The array_count_values() function returns the number of occurrence of each unique value in the array.

Syntax:

array_count_values(array)

Example:

$x = array("a","b","c","a","a","b");
print_r(array_count_values($x));

Output:

Array ( [a] => 3 [b] => 2 [c] => 1 )

The array_diff() Function

The array_diff() function is used to compare the values of the two arrays, and it returns the difference between them.

Syntax:

array_diff(arr1,arr2,…etc)

Example:

$x1 = array("1","2","3","4");
$x2 = array("1","2","4");
print_r(array_diff($x1,$x2));

Output:

Array ( [2] => 3 )

The array_diff_assoc() Function

The array_diff_assoc() function is used to compare the keys and the values of the two arrays, and it returns the difference between them.

Syntax:

array_diff_assoc(arr1,arr2,…etc)

Example:

$x1 = array("1"=>"Nick","2"=>"Yash","3"=>"Monil","4"=>"Shubham");
$x2 = array("1"=>"Nick","2"=>"Monil","3"=>"Monil");
print_r(array_diff_assoc($x1,$x2));

Output:

Array ( [4] => Shubham )

The array_diff_key() Function

The array_diff_key() function is used to compare the keys of the two arrays, and it returns the difference between them.

Syntax:

array_diff_key(arr1,arr2,…etc)

Example:

$x1 = array("1"=>"Nick","2"=>"Yash","3"=>"Monil","4"=>"Shubham");
$x2 = array("1"=>"Nick","2"=>"Monil","3"=>"Monil");
print_r(array_diff_key($x1,$x2));

Output:

Array ( [4] => Shubham )

The array_fill() Function

The array_fill() function is used to fill array positions with particular values.

Syntax:

array_fill(index,number,value)

Note:

  • The index parameter indicates the index no from where we want to start index.
  • The number parameter indicates the number of elements from where we want to insert.
  • The value parameter is the value of the position.

Example:

$x = array_fill(2,3,"Errorsea");

Output:

Array ( [2] => Errorsea [3] => Errorsea [4] => Errorsea)

The array_fill_keys() function

The array_fill_keys() function returns a new array with the key values of passed array and sets value of each key as specified value.

Syntax:

array_fill_keys(keys, value)

Example:

$name=array("Nick","Yash","Monil");
print_r(array_fill_keys($name,18));

Output:

Array ( [Nick] => 18 [Yash] => 18 [Monil] => 18 )

The array_flip() Function

The array_flip() function flips the keys with their values in the array.

Syntax:

array_flip(array)

Example:

$x = array("name"=>"Nick","age"=>18,"eye color"=>"black");
print_r(array_flip($x));

Output:

Array ( [Nick] => name [18] => age [black] => eye color )

The array_intersect() Function

The array_intersect() function is used to compare the values of the two arrays, and it returns the matches.

Syntax:

array_intersect(arr1,arr2,…etc)

Example:

$x1 = array(1,2,3,4);
$x2 = array(1,2,4);
print_r(array_intersect($x1,$x2));

Output:

Array ( [0] => 1 [1] => 2 [3] => 4 )

The array_intersect_assoc() Function

The array_intersect_assoc() function is used to compare the keys and the values of the two arrays, and it returns the matches.

Syntax:

array_intersect_assoc(arr1,arr2,…etc)

Example:

$x1 = array("1"=>"Nick","2"=>"Yash","3"=>"Monil","4"=>"Shubham");
$x2 = array("1"=>"Nick","2"=>"Monil","3"=>"Monil");
print_r(array_intersect_assoc($x1,$x2));

Output:

Array ( [1] => Nick [3] => Monil )

The array_intersect_key() Function

The array_intersect_key() function is used to compare the keys of the two arrays, and it returns the matches.

Syntax:

array_intersect_key(arr1,arr2,…etc)

Example:

$x1 = array("1"=>"Nick","2"=>"Yash","3"=>"Monil","4"=>"Shubham");
$x2 = array("1"=>"Nick","3"=>"Monil");
print_r(array_intersect_key($x1,$x2));

Output:

Array ( [1] => Nick [3] => Monil )

The array_key_exists() Function

The array_key_exist() function is used to search a particular key in the array. It returns true if the key is present in the array. It returns false if the key isn’t present in the array.

Syntax:

array_key_exists(key,array)

Example:

$x = array("name"=>"Nick","age"=>18,"eye color"=>"black");
if(array_key_exists("name",$x))
{
echo "key is present";
}else{
echo "key isn’t present";
}

Output:

Key is present

The array_keys() Function

The array_keys() function returns the array keys of specified array.

Syntax:

array_keys(array,value,strict)

Note: Here value and strict fields are optional.

Example:

$x = array("name"=>"Nick","age"=>18,"eye color"=>"black");
print_r(array_keys($x));

Output:

Array ( [0] => name [1] => age [2] => eye color )

The array_map() Function

This function used for sending values of the array to a user-created function and returns the array with new values.

Syntax:

array_map(function,arr1,arr2,…etc)

Example:

$x = array(1,2,4);
function cube($m){
Return $m*$m*$m;
}
print_r((array_map(cube,$x));

Output:

Array ( [0] => 1 [1] => 8 [2] => 64 )

The array_merge() Function

The array_merge() function is used to merge two or more arrays.

Syntax:

array_merge(arr1,arr2,…etc)

Example:

$x1 = array(1,2,4);
$x2 = array(6,7);
print_r(array_merge($x1,$x2));

Output:

Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 6 [4] => 7 )

The array_multisort() Function

The array_multisort() function returns the sorted array in ascending order.

Example:

$x = array("a","y","c","k","h","z");
print_r(array_multisort($x));

Output:

Array ( [0] => a [1] => c [2] => h [3] => k [4] => y [5] => z )

The array_pad() Function

The array_pad() function also inserts a specific number of elements with specific values.

Syntax:

array_pad(array,size,value)

Example:

$x = array("Yash","Nick","Monil")
print_r(array_pad($x,5,"Errorsea"));

Output:

Array ( [0] => Yash [1] => Nick [2] => Monil [3] => Errorsea [4] => Errorsea )

The array_product() Function

The array_product() function returns the product of array elements.

Syntax:

array_product(array)

Example:

$x = array(2,5);
print_r(array_product($x));

Output:

10

The array_push() Function

The array_push() function pushes the new element at the end of the array.

Syntax:

array_push(array,val2,val2,…etc)

Example:

$x = array(1,8,3);
print_r(array_push(4,6));

Output:

Array ( [0] => 1 [1] => 8 [2] => 3 [3] => 4 [4] => 6 )

The array_pop() Function

The array_pop() function pops the last element of the array.

Syntax:

array_pop(array)

Example:

$x = array(1,2,3);
print_r(array_pop());

Output:

Array ( [0] => 1 [1] => 2 )

The array_rand() Function

The array_rand() function returns the random keys from the array. We can specify the number of random keys we want.

Example:

$x = array(1,2,3,4,5);
$random = aray_rand($x,2);
echo($x[$random[0]])."<br>";
echo($x[$random[1]]);

Output:

2
4

The array_replace() Function

The array_replace() function replaces the values of the first array with the values of the second array.

Syntax:

array_replace(arr1,arr2)

Example:

$x1 =array(1,2);
$x2 =array(3,4);
print_r($x1,$x2);

Output:

Array ( [0] => 3 [1] => 4 )

The array_reverse() Function

The array_reverse() function reverses the array element.

Syntax:

array_reverse(array);

Example:

$x = array("A","B","C");
print_r(array_reverse($x));

Output:

Array ( [0] => C [1] => B [2] => A )

The array_search() Function

The array_search() function is used to search the value in the array, it returns the key position of the matching element.

Syntax:

array_search(value, array, strict);

Note: Here strict is an optional field.

Example:

$x = array("name"=>"Nick","age"=>18,"eye color"=>"black");
echo array_search("Nick",$x);

Output:

name

The array_shift() Function

The array_shift() function removes the first element of an array and returns it as a result.

Syntax:

array_shift(array)

Example:

$x = array(1,2,3,4,5)
echo array_shift($x);
print_r($x);

Output:

1
Array ( [0] => 2 [1] => 3 [2] => 4, [3] => 5 )

The array_slice() Function

The array_slice() function returns the selected part of the array.

Syntax:

array_slice(array, start, length, preserve)

Note: Here length and preserve are optional fields.

Example:

$person = array("yash","monil","Nick","Shubham");
print_r(array_slice($person,2));

Output:

Array ( [0] => Nick [1] => Shubham )

The array_splice() Function

The array_splice() function removes selected elements from the first array and replaces it with new elements of the second array.

Syntax:

array_splice(array, start, length, array)

Example:

$person = array("yash","monil","Nick","Shubham");
$x = array(1,2);
array_splice($person,0,2,$x);
print_r($person);

Output:

Array ( [0] => 1 [1] => 2 [2] => Nick [3] => Shubham )

The array_sum() Function

The array_sum() function returns the sum of the array elements.

Syntax:

array_sum(array)

Example:

$x = array(1,2,3,4,5);
echo array_sum($x);

Output:

15

The array_values() Function

The array_values() function returns the array containing the values of an array.

Syntax:

$x = array("name"=>"Nick","age"=>18,"eye color"=>"black");
print_r(array_values($x));

Output:

Array ( [0] => Nick [1] => 18 [2] => black )

The array_walk() Function

The array_walk() function runs all array element’s keys and values into user-defined functions.

Syntax:

array_walk(array,function)

Example:

function test($val,$key)
{
echo "The key $key has the value $val <br>";
}
$x = array("name"=>"Nick","age"=>18,"eye color"=>"black");
print_r(array_values($x));

Output:

The key name has the value Nick
The key age has the value 18
The key eye color has the value black

The adsort() Function

The adsort() function sorts the array according to the values in descending order.

Syntax:

adsort(array,sort_type)

Note: Sort_type is optional.

Example:

$person = array("yash" => 18 , "Nick" => 21 , "Monil" => 15 );
adsort($person);
foreach($person as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value."<br>";
}

Output:

Key=Nick, Value=21
Key=yash, Value=18
Key=Monil, Value=15

The asort() Function

The asort() function sorts the array according to the values in ascending order.

Syntax:

asort(array,sort_type)

Note: Sort_type is optional.

Example:

$person = array("yash" => 18 , "Nick" => 21 , "Monil" => 15 );
asort($person);
foreach($person as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value."<br>";
}

Output:

Key=Monil, Value=15
Key=yash, Value=18
Key=Nick, Value=21

The compact() Function

The compact() function is used to create an array from given variable values.

Syntax:

compact(var1,var2,….)

Example:

$name="Nick";
$age=20;
$eye_colour = "black";
print_r(compact("name","age","eye_colour"));

Output:

Array ( [name] => Nick [age] => 20 [eye colour] => black )

The count() Function

The count() function returns the number of elements in the array.

Syntax:

count(array, mode)

Node: The mode is an optional field

Example:

$m = array(1,2,3,5,6);
echo count($m);

Output:

6

The current() Function

The current() function returns the value of the current elements in the array.

Syntax:

current(array)

Example:

$m = array(1,2,3,5,6);
echo currnt($m);

Output:

1

The each() Function

The each() function returns the current element key and value. It also moves the internal pointer forward.

Syntax:

each(array)

Example:

$x = array("yash" => 18 , "Nick" => 21 , "Monil" => 15 );
print_r(each($x));

Output:

Array ( [1] => 18 [value] => 18 [0] => yash [key] => yash )

The end() Function

The end() function moves the internal pointer to the last element and returns it.

Syntax:

end(array)

Example:

$x = array("yash", "Nick" , "Monil" );
echo end($x);

Output:

Monil

The in_array() Function

The in_array() function is used to search a particular value in the array.

Note:

  • It returns true if the key is present in the array.
  • It returns false if the key isn’t present in the array.

Syntax:

in_array(search, array,type)

Example:

$x = array(1,2,3,4,5);
if(array_key_exists(4,$x))
{
echo " Value is present";
}else{
echo "Value isn’t present";
}

Output:

Value is present

The key() Function

The key() function returns the key at the current position in the array.

Syntax:

key(array)

Example:

$x = array("yash" => 18 , "Nick" => 21 , "Monil" => 15 );
echo "key is " .key($x);

Output:

key is yash

The krsort() Function

The krsort() function sorts the array according to the keys in descending order.

Syntax:

krsort(array,sort_type)

Note: Sort_type is optional.

Example:

$person = array("yash" => 18 , "Nick" => 21 , "Monil" => 15 );
krsort($person);
foreach($person as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value."<br>";
}

Output:

Key=Yash, Value=18
Key=Nick, Value=21
Key=Monil, Value=15

The ksort() Function

The ksort() function sorts the array according to the keys in ascending order.

Syntax:

ksort(array,sort_type)

Note: Sort_type is optional.

Example:

$person = array("yash" => 18 , "Nick" => 21 , "Monil" => 15 );
ksort($person);
foreach($person as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value."<br>";
}

Output:

Key=Monil, Value=15
Key=Nick, Value=21
Key=Yash, Value=18

The list() Function

The list() function is used to assign values of an array to the variables.

Syntax:

list(val1,val2,….)

Example:

$x = array("Yash","Nick","Monil");
list($a,$b,$c) = $x;
echo $a." ".$b." ".$c;

Output:

Yash Nick Monil

The next() Function

The next() function moves the internal pointer to the next element in the array.

Syntax:

next(array)

Example:

$x = array("Yash","Nick","Monil");
echo(next($x));

Output:

Nick

The pos() Function

The post() function returns the current element of the array.

Syntax:

pos(array)

Example:

$x = array("Yash","Nick","Monil");
echo(pos($x));

Output:

Yash

The prev() Function

The prev() function first moves the internal pointer to the previous element.

Syntax:

prev(array)

Example:

$x = array("Yash","Nick","Monil");
echo(current($x))."<br>";
echo(next($x))."<br>";
echo(prev($x));

Output:

Yash
Nick
Yash

The range() Function

The range() function creates the new array containing the range of array elements.

Syntax:

range(low,high)

Example:

print_r(range(1,3));

Output:

Array ( [0] => 1 [1] => 2 [2] => 3 )

The reset() Function

The reset() function resets the internal pointer to the first position.

Syntax:

reset(array)

Example:

$x = array("Yash","Nick","Monil");
echo(current($x))."<br>";
echo(next($x))."<br>";
echo(reset($x));

Output:

Yash
Nick
Yash

The sort() Function

The sort() function sorts the indexed array in ascending order.

Syntax:

sort(array,sort_type)

Note: Here sort_type is optional

Example:

$x = array("Yash","Nick","Monil");
sort($x);
print_r($x);

Output:

Array ( [0] => Monil [1] => Nick [2] => Yash )

The rsort() Function

The rsort() function sorts the indexed array in descending order.

Syntax:

rsort(array,sort_type)

Note: Here sort_type is optional

Example:

$x = array("Yash","Nick","Monil");
rsort($x);
print_r($x);

Output:

Array ( [0] => Yash [1] => Nick [2] => Monil )

The shuffle() Function

The shuffle() function sets the array elements in the random order.

Syntax:

shuffle(array)

Example:

$x = array("Yash","Nick","Monil");
shuffle($x);
print_r($x);

Output:

Array ( [0] => Nick [1] => Monil [2] => Yash )

The sizeof() Function

The sizeof() function returns the size of the array.

Syntax:

sizeof(array,mode)

Example:

$x = array("Yash","Nick","Monil");
echo sizeof($x);

Output:

3

Conclusion

These are the different types of array function which are useful in learning PHP programming. I hope now you have a basic understanding of PHP array functions.

Enjoy Programming 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *