PHP Array Functions

PHP array functions are helpful to perform various complicated operations on PHP arrays. Also, PHP array functions are effortless to implement.

Read Also: PHP Arrays

Array Functions in PHP

Even though there are three types of arrays in PHP, there is no difference in applying any of the array functions to them.

The array() Function

The array() function is used to create and initialize a PHP array. This function is useful to create indexed arrays or associative arrays. PHP arrays could be single-dimensional or multidimensional.

Parameters

Sr.NoParameter & Description
1key(Optional)

It specifies the key, of type numeric or string.

2value(Required)

It specifies the value.

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

This function changes the case of all keys of the array and returns an array with all its keys either in lower case or in upper case.

There are two parameters to be passed in function, which are as below.

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

Parameters

Sr.NoParameter & Description
1input (Mandatory)

This is the array for which we want to change the case of all the keys.

2case (Optional)

This will take constant value, either CASE_UPPER or CASE_LOWER.

Syntax

array_change_key_case(array,case)

Example

<?php

$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 takes an array as input and splits it into arrays of smaller chunks of the given size.

Syntax

array_chunk(array,size)

Parameters

Sr.NoParameter & Description
1input (mandatory)

This is the input array that we want to slipt into smaller chunks. This is a mandatory parameter.

2size (mandatory)

The size of each chunk we want to split our passed array in the form of input. This is again a mandatory parameter.

3preserve_keys (optional)

This is an optional and boolean parameter, but when it is set to TRUE, all the keys in the array will be preserved. If you do not pass it, then its default value is FALSE, which will reindex the chunk numerically.

Example

<?php

$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 values from a single column of the input array and is identified by the column_key.

Syntax

array_column(array,column_key,index_key)

Parameters

Sr.NoParameter & Description
1input (mandatory)

A multidimensional array or an array of objects from which a column of values is pulled.

2column_key (mandatory)

The column of values to return. This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name. This value can be NULL to return complete arrays or objects

3index_key (optional)

The column to use as the index/keys for the returned array. This value may be the integer key of the column, or it may be the string key name.

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

Example

<?php

$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",

    )

);

$_names = array_column($person, 'name');

print_r($_names);

?>

Output

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

The array_combine() Function

This function takes two different or the same arrays as input and creates a new array by using the values from the key array as keys and the values from the values array as the corresponding values.

Syntax

array_combine(keys,values)

Parameters

Sr.NoParameter & Description
1keys (mandatory)

First, the array whose values will be used as the keys to creating a new array.

2values (mandatory)

Second, the array whose values will be used as the values to create the new array.

Example

<?php

$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 an associative array of values using the values of the input array as keys and their frequency in the input array as values.

Syntax

array_count_values(array)

Parameters

Sr.NoParameter & Description
1input (mandatory)

The input array of values to count

Example

<?php

$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 compares array1 against one or more other arrays passed to it and returns the values in array1 that are not present in any of the other arrays.

Syntax

array_diff(arr1,arr2,…etc)

Parameters

Sr.NoParameter & Description
1array1 (Required)

This is the first array that will be compared with other arrays passed to the function.

2array2 (Required)

This is an array to be compared with the first array

3array3 (Optional)

This is the second array to be compared with the first array

4More Arrays (Optional)

You can pass more number of arrays you want to compare with the first input array.

Example

<?php

$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 function compares two or more arrays and returns the difference.

This function compares the keys and the values of two (or more) arrays and returns an array that contains the entries from array1 but that are not present in array2 or array3, etc.

This function is different from than array_diff() function because array_diff() uses only values to compare with other arrays, whereas the array_diff_assoc() function uses keys and values while comparing with other arrays.

Syntax

array_diff_assoc(arr1,arr2,…etc)

Parameters

Sr.NoParameter & Description
1array1 (Required)

It is an array to compare from

2array2 (Required)

It is an array to be compared with the first array

3array3 (Optional)

It is an array to be compared with the first array

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 ( [2] => Yash [4] => Shubham )

The array_diff_key() Function

The array_diff_key() function compares the keys from array1 against the keys from array2, array3…etc and returns an array having the difference, i.e. the keys which are available in array1 and not available in array2, array3…etc. This function is like the array_diff() function except the comparison is done based on the keys instead of the values.

Syntax

array_diff_key(arr1,arr2,…etc)

Parameters

Sr.NoParameter & Description
1array1(Required)

This first array will be compared with.

2array2(Required)

It is an array to be compared with the first array

3array3(Optional)

It is an array to be compared with the first array

Example

<?php

$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

It fills an array with num entries of the value parameter, keys starting at the start_index parameter.

Syntax

array_fill(index,number,value)

Parameters

Sr.NoParameter & Description
1start_index

The first index of the returned array

2num

It contains the number of elements to insert

3value

It contains the value to be inserted

Note:

  • The index parameter indicates the index no from where we want to start the 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

<?php

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

print_r($x);

?>

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 the passed array and sets the value of each key as the specified value.

Syntax

array_fill_keys(keys, value)

Parameters

Sr.NoParameter & Description
1keys

It is an array of values that will be used as keys

2value

It may be a string or an array of value

Example

<?php

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

print_r(array_fill_keys($name,18));

?>

Output

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

The array_flip() Function

It returns an array in flip order, i.e. keys from input become values and values from input become keys.

Syntax

array_flip(array)

Parameters

Sr.NoParameter & Description
1input

The array to be fliped

Example

<?php

$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 is used to return the common terms.

Syntax

array_intersect(arr1,arr2,…etc)

Parameters

Sr.NoParameter & Description
1array1(Required)

The first array is the array that the others will be compared with.

2array2(Required)

This is an array to be compared with the first array

3array3(Optional)

This an array to be  compared with the first array

Example

<?php

$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)

Parameters

Sr.NoParameter & Description
1array1(Required)

The first array is the array that the others will be compared with.

2array2(Required)

This is an array to be compared with the first array

3array3(Optional)

This is an array to be compared with the first array

Example

<?php

$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 useful to compare the keys of the two arrays, and it returns the matches.

Syntax

array_intersect_key(arr1,arr2,…etc)

Parameters

Sr.NoParameter & Description
1array1(Required)

The first array is the array that the others will be compared with.

2array2(Required)

This is an array to be compared with the first array

3array3(Optional)

This is an array to be compared with the first array

Example

<?php

$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 useful 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)

Parameters

Sr.NoParameter & Description
1key(Required)

The key to be searched.

2array(Required)

This is an array to be searched.

Example

<?php

$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 the specified array.

Syntax

array_keys(array,value,strict)

Parameters

Sr.NoParameter & Description
1input(Required)

It specifies an array.

2search_value(Required)

You can specify a value, then only the keys with this value are returned.

3strict

Optional. Used with the value parameter.

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 is 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)

Parameters

Sr.NoParameter & Description
1callback(Required)

The name of the user-made function, or null.

2array1(Required)

It specifies an array.

3array2(Optional)

It specifies an array.

4array3(Optional)

It specifies an array.

Example

<?php

$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)

Parameters

Sr.NoParameter & Description
1callback(Required)

The name of the user-made function, or null.

2array1(Required)

It specifies an array.

3array2(Optional)

It specifies an array.

4array3(Optional)

It specifies an array.

Example

<?php

$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.

Parameters

Sr.NoParameter & Description
1array1(Required)

It specifies an array

2Sort order(Optional)

It specifies the sorting order. Possible values −

  • SORT_ASC Default. Sort in ascending order (A-Z)
  • SORT_DESC sort in descending order (Z-A)
3Sorting type(Optional)

It specifies the type to use when comparing elements. Possible values −

  • SORT_REGULAR Default. Compare elements normally
  • SORT_NUMERIC Compare elements as numeric values
  • SORT_STRING Compare elements as string values
4array2(Optional)

It specifies an array

Example

<?php

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

array_multisort($x);

print_r($x);

?>

Output

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

The array_pad() Function

It returns a copy of the array padded to the size specified by pad_size with value pad_value.

Syntax

array_pad(array,size,value)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array.

2pad_size(Required)

It specifies the number of elements in the array returned from the function.

3pad_value(Required)

It specifies the value of the new elements in the array returned from the function.

Example

<?php

$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)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array.

Example

<?php

$x = array(2,5);

print_r(array_product($x));

?>

Output

10

The array_push() Function

This function treats the array as a stack and pushes the passed variables var1, var2… onto the end of the array.

Syntax

array_push(array,val2,val2,…etc)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array.

2var1(Required)

The value to be pushed.

3var2(Optional)

The value to be pushed.

Example

<?php

$x = array(1,8,3);

array_push($x,4,6);

print_r($x);

?>

Output

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

The array_pop() Function

The function pops and returns the last value of the array.

Syntax

array_pop(array)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array.

Example

<?php

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

array_pop($x);

print_r($x);

?>

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.

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array.

2num_req(Optional)

It specifies how many entries you want to pick – if not specified, it defaults to 1.

Example

<?php

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

$random = array_rand($x,2);

echo $x[$random[0]]."<br>";

echo $x[$random[1]];

?>

Output

3
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

<?php

$x1 =array(1,2);

$x2 =array(3,4);

print_r(array_replace($x1,$x2));

?>

Output

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

The array_reverse() Function

This function reverses the order of all the elements of a padded array.

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array.

2preserve_keys(Optional)

It specifies if the order of keys also has to be changed or not. By default, it’s FALSE.

Syntax

array_reverse(array)

Example

<?php

$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)

Parameters

Sr.NoParameter & Description
1value(Required)

It specifies a value to be searched.

2array(Required)

It specifies an array.

3strict(Optional)

If it is set to TRUE, then the array_search() will also check the types of the search in the array.

Note: Here, strict is an optional field.

Example

<?php

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

echo array_search("Nick",$x);

?>

Output

name

The array_shift() Function

This function shifts the first value of the array off and returns it, shortening the array by one element and moving everything down.

Syntax

array_shift(array)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array.

Example

<?php

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

echo array_shift($x)."<br>";

print_r($x);

?>

Output

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

The array_slice() Function

The function returns the sequence of elements from the array as specified by the offset and length parameters.

Syntax

array_slice(array, start, length, preserve)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array.

2offset(Required)

It specifies where the function will start the slice.

3length(Optional)

It specifies the length of the slice.

4preserve_keys(Optional)

It gives TRUE to preserve keys and FALSE to reset keys. Default is FALSE.

Note: Here length and preserve are optional fields.

Example

<?php

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

print_r(array_slice($person,2));

?>

Output

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

The array_splice() Function

This function removes the elements designated by offset and length from the input array, and replaces them with the elements of the replacement array, if supplied. It returns an array containing the extracted elements.

Syntax

array_splice(array, start, length, array)

Parameters

Sr.NoParameter & Description
1input(Required)

It specifies an array

2offset

It specifies where the function will start removing elements. 0 = the first element.

3length(Optional)

It specifies how many elements will be removed, and also the length of the returned array.

4replacement(Optional)

It specifies an array with the elements that will be inserted into the original array.

Example

<?php

$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

It calculates the sum of values in an array and returns the sum of values in an array as an integer or float.

Syntax

array_sum(array)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array.

Example

<?php

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

echo array_sum($x);

?>

Output

15

The array_values() Function

This function returns all the values from the input array and indexes numerically the array.

Syntax

array_values ( $array )

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array.

Example

<?php

$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

This function returns an array containing all the values of array1 that are present in all the arguments array2, array3.

Syntax

array_walk(array,function)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array.

2funcname(Required)

The name of the user-made function.

3parameter(Optional)

It specifies a parameter to the user-made function.

Example

<?php

function test($val,$key)

{

    echo "The key $key has the value $val<br>";

}

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

array_walk($x, "test");

?>

Output

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

The asort() Function

This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant. The adsort() function is an alternative for the asort() function.

Syntax

asort(array,sort_type)

Parameters

Sr.NoParameter & Description
1array

Required. Specifies an array.

2sort_flags

Optional. Specifies how to sort the array values. Possible values −

  • SORT_REGULAR − Default. Treat values as they are (don’t change types)
  • SORT_NUMERIC − Treat values numerically
  • SORT_STRING − Treat values as strings
  • SORT_LOCALE_STRING − Treat values as strings, based on local settings

Example

<?php

$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

This function takes a variable number of parameters. Each parameter can be either a string containing the name of the variable, or an array of variable names.

Syntax

compact(var1,var2,….)

Parameters

Sr.NoParameter & Description
1var1(Required)

It can be a string with the variable name or an array of variables.

2var2(Optional)

It can be a string with the variable name or an array of variables.

Example

<?php

$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

In this function if the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array.

Syntax

count(array, mode)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array

2mode(Optional)

It specifies the mode of the function.

Node: The mode is an optional field

Example

<?php

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

echo count($m);

?>

Output

5

The current() Function

Every array has an internal pointer to its “current” element, which is initialized to the first element inserted into the array.

Syntax

current(array)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array

Example

<?php
$m = array(1,2,3,5,6);
echo current($m);
?>

Output

1

The each() Function

The each() function returns the current key and value pair from the array and advances the array cursor. This pair is returned in a four-element array, with the keys 0, 1, key, and value. Elements 0 and key contain the key name of the array element, and 1 and value contain the data.

Syntax

each(array)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array

Example

<?php

$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

This function moves the internal pointer to the last element and returns it.

Syntax

end(array)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array

Example

<?php
$x = array("yash", "Nick" , "Monil" );
echo end($x);
?>

Output

Monil

The in_array() Function

This function searches an array for a specific value.

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)

Parameters

Sr.NoParameter & Description
1value(Required)

The value to be searched in the array.

2array(Required)

It specifies an array

3strict(Optional)

If this parameter is set, the in_array() function searches for the search-string and specific type in the array.

Example

<?php

$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 element key from the current internal pointer position.

Syntax

key(array)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array

Example

<?php

$x = array("Yash" => 18 , "Nick" => 21 , "Monil" => 15 );

echo "key is " .key($x);

?>

Output

key is Yash

The krsort() Function

This function sorts an array by the keys in reverse order. The values keep their original keys.

Syntax

krsort(array,sort_type)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array

2sort_flag(Optional)

It specifies how to sort the array values. Possible values −

  • SORT_REGULAR − Default. Treat values as they are (don’t change types)
  • SORT_NUMERIC − Treat values numerically
  • SORT_STRING − Treat values as strings
  • SORT_LOCALE_STRING − Treat values as strings, based on local settings

Example

<?php

$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

This function sorts the array by key, maintaining key to data correlations. This is useful mainly for associative arrays.

Syntax

ksort(array,sort_type)

Parameters

Sr.NoParameter & Description
1array

Required. Specifies an array

2sort_flag

Optional. Specifies how to sort the array values. Possible values −

  • SORT_REGULAR − Default. Treat values as they are (don’t change types)
  • SORT_NUMERIC − Treat values numerically
  • SORT_STRING − Treat values as strings
  • SORT_LOCALE_STRING − Treat values as strings, based on local settings


Example

<?php

$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

Like array(), this is not really a function, but a language construct. list() is used to assign a list of variables in one operation.

Syntax

list(val1,val2,….)

Parameters

Sr.NoParameter & Description
1var1(Required)

The first variable to assign a value to

2var2(Optional)

The second variable to assign a value to

3var3(Optional)

The third variable to assign a value to

Example

<?php

$x = array(“Yash”,”Nick”,”Monil”);

list($a,$b,$c) = $x;

echo $a.” “.$b.” “.$c;

?>

Output

Yash Nick Monil

The next() Function

This function returns the array value in the next place or FALSE if there are no more elements.

Syntax

next(array)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array

Example

<?php

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

echo(next($x));

?>

Output

Nick

The pos() Function

This function returns the value of the array element. The function does not move the pointer. If the pointer points beyond the end of the elements list, pos() returns FALSE.

Syntax

pos(array)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array

Example

<?php

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

echo(pos($x));

?>

Output

Yash

The prev() Function

This function returns the array value in the previous place or FALSE if there are no more elements.

Syntax

prev(array)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array

Example

<?php

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

echo(current($x))."<br>";

echo(next($x))."<br>";

echo(prev($x));

?>

Output

Yash
Nick
Yash

The range() Function

The function returns an array of elements from low to high, inclusive. If low > high, the sequence will be from high to low.

Syntax

range(low,high)

Parameters

Sr.NoParameter & Description
1low(Required)

This is a lower range of the array

2high(Required)

This is an upper range of the array

3step(Optional)

Steps to increase the array element. By default, it is 1

Example

<?php

print_r(range(1,3));

?>

Output

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

The reset() Function

The reset() function rewinds the array’s internal pointer to the first element and returns the value of the first array element, or FALSE if the array is empty.

Syntax

reset(array)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array

Example

<?php

$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 an array. In this function, the Elements will be arranged from lowest to highest.

Syntax

sort(array,sort_type)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array.

2sort_flags(Optional)

It specifies how to sort the array values. Possible values −

  • SORT_REGULAR − Default. Treat values as they are (don’t change types)
  • SORT_NUMERIC − Treat values numerically
  • SORT_STRING − Treat values as strings
  • SORT_LOCALE_STRING − Treat values as strings, based on local settings

Example

<?php

$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)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array.

2sort_flags(Optional)

It specifies how to sort the array values. Possible values −

  • SORT_REGULAR − Default. Treat values as they are (don’t change types)
  • SORT_NUMERIC − Treat values numerically
  • SORT_STRING − Treat values as strings
  • SORT_LOCALE_STRING − Treat values as strings, based on local settings

Example

<?php

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

rsort($x);

print_r($x);

?>

Output

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

The shuffle() Function

This function shuffles an array. It assigns new keys for the elements in the array. It removes any existing keys you may have assigned, rather than just reordering the keys.

Syntax

shuffle(array)

Parameters

Sr.NoParameter & Description
1array(Required)

It specifies an array.

Example

<?php

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

shuffle($x);

print_r($x);

?>

Output

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

The sizeof() Function

In this function iff, the optional mode parameter is set to COUNT_RECURSIVE sizeof() will recursively count the array. The function is useful for counting all the elements of a multidimensional array. The default value for mode is 0.

Syntax

sizeof(array,mode)

Parameters

Sr.NoParameter & Description
1array

Required. Specifies an array

2mode

Optional. Specifies the mode of the function.

Example

<?php

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

echo sizeof($x);

?>

Output

3

Read More: PHP Superglobals

Conclusion

These are the different types of array function which are useful in learning PHP programming. We hope we have been able to help you have in gaining a basic understanding of PHP array functions. 

Enjoy Programming 🙂

Leave a Reply

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