We are going to covered Top 16 Most Useful PHP Array Functions which frequently use in the development of PHP application or asked in the interview questions. These all are the in-built PHP functions.
- array_values()
- array_keys()
- array_key_exists()
- array_shift()
- array_unshift()
- array_push()
- array_pop()
- array_unique()
- array_slice()
- array_diff()
- array_search()
- array_reverse()
- array_flip()
- array_replace()
- is_array()
- in_array()
array_values() Function
We use
array_valuees()
function to get only values from the array. It does not return the key of the array.
Syntax:
1 | array_values(Input); |
Input: This function only takes one mandatory array parameter, from which values will be extracted.
Example:
1 2 3 4 5 | <?php $array = array("John" => 900, "Peter" => 8000, "Kris" => 5000); $values = array_values($array); print_r($values); ?> |
Output: The function
array_values()
extract the values of an array.
1 2 3 4 5 6 | Array ( [0] => 900 [1] => 8000 [2] => 5000 ) |
array_keys() Function
We use the
array_keys()
function to extract the key of the array.
Syntax:
1 | array_keys(Input, Value, Strict); |
It takes three parameters and only the first parameter is required.
Input: It is an array, from which keys will be extracted.
Value: If you add value, then only the key of this value will be returned.
Strict: It is a boolean parameter and the default value is FALSE. If you set it TRUE then it works as a strict comparison operator(===).
Example:
1 2 3 4 5 | <?php $array = array("John" => 900, "Peter" => 8000, "Kris" => 5000); $keys = array_keys($array); print_r($keys); ?> |
Output: Here is showing the keys of the array.
1 2 3 4 5 6 | Array ( [0] => John [1] => Peter [2] => Kris ) |
Let’s take an example to passing value:
1 2 3 4 5 | <?php $array = array("John" => 900, "Peter" => 8000, "Kary" => 8000, "Kris" => 5000); $keys = array_keys($array, 8000); print_r($keys); ?> |
Output: It will return the keys of value 8000.
1 2 3 4 5 | Array ( [0] => Peter [1] => Kary ) |
array_key_exists() Function
We use
array_key_exists()
function to check the specific key in an array. It returns TRUE if the key exists.
Syntax:
1 | array_key_exists(Key, Array) |
It takes two parameters:
Key: The key, you have to check.
Array: The key will be searched in this array.
Example:
1 2 3 4 5 | <?php $name = array("John" => 300, "Peter" => 400); $result = array_key_exists('John', $name); echo $result; ?> |
array_shift() Function
It is used to remove the first element from an array and returns the removed element ‘s value.
Syntax:
1 | array_shift(array) |
It takes an array as a parameter.
Example:
1 2 3 4 5 6 7 | <?php $name = array("John" => 300, "Peter" => 400); $result = array_shift($name); echo $result; echo "<br>"; print_r($name); ?> |
Output: In this example, the
array_shift()
function returns the value of the first element and also removed the first element of the “$name” array.
1 2 3 4 5 | 300 Array ( [Peter] => 400 ) |
array_unshift() Function
We use the
array_shift()
function to add one or more elements to the array. The new elements will be added at the beginning of the array.
Syntax:
1 | array_shift(array, Value1, Value2, ....) |
Example: Here is taken an array of three elements and adding two new elements using the
array_unshift()
function.
1 2 3 4 5 | <?php $array = array('A', 'B', 'C'); array_unshift($array, 'D', 'E'); print_r($array); ?> |
Output: The new two elements are added at the beginning of the array.
1 2 3 4 5 6 7 8 | Array ( [0] => D [1] => E [2] => A [3] => B [4] => C ) |
array_push() Function
We use
array_push()
function to add one or more elements at the end of the array.
Syntax:
1 | array_push(Array, Values) |
Array: This will be appended.
Values: The values will add to the array.
Example: Here is taken an example of an array that has three elements and adding two new elements using the
array_push()
function.
1 2 3 4 5 | <?php $array = array(100, 200, 50); array_push($array, 70, 80); print_r($array); ?> |
Output: Now two more elements have been pushed to the array at the end of the array.
1 2 3 4 5 6 7 8 | Array ( [0] => 100 [1] => 200 [2] => 50 [3] => 70 [4] => 80 ) |
array_pop() Function
We use
array_pop()
function to remove the last element of the array.
Syntax:
1 | array_pop(array) |
Example: In the below example, take an array with five elements.
1 2 3 4 5 | <?php $array = array(100, 200, 50, 300, 400); array_pop($array); print_r($array); ?> |
Output: This function removed the last element from the array.
1 2 3 4 5 6 7 | Array ( [0] => 100 [1] => 200 [2] => 50 [3] => 300 ) |
array_unique() function
We use the
array_unique()
function to remove duplicate value from an array.
1 | array_unique(array) |
Example: In this example, take 5 elements of the array and the value of the two-element is the same.
1 2 3 4 5 | <?php $array = array("a" => 100, "b" => 200, "c" => 50, "d" => 300, "e" => 100); $data = array_unique($array); print_r($data); ?> |
Output: The duplicate value has been removed.
1 2 3 4 5 6 7 | Array ( [a] => 100 [b] => 200 [c] => 50 [d] => 300 ) |
array_slice() Function
We use the
array_slice()
function for slicing the array.
Syntax:
1 | array_slice(Array, Offset, Length, Preserve) |
It accepts 4 arguments:
Array: This is the array for slicing.
Offset: The array start where the slicing is to take place. It displays location in the array. If Offset is negative (-1), then the slicing will start from the end of the array. The value -3 means that start at the array ‘s third last element.
Length: This is a slice length.
Preserve: It is a boolean parameter and the default value is FALSE. If you set it TRUE then it preserves the key values.
Example:
1 2 3 4 5 | <?php $array = array(100, 200, 50, 300, 100); $data = array_slice($array, 2); print_r($data); ?> |
Output: This function slice the first two elements of the array and reset the key.
1 2 3 4 5 6 | Array ( [0] => 50 [1] => 300 [2] => 100 ) |
Let’s take another example:
1 2 3 4 5 | <?php $array = array(100, 200, 50, 300, 100); $data = array_slice($array, 1, 3, TRUE); print_r($data); ?> |
Output: In this example, the function return values from 200, 50, and 300. The key has not reset because the argument of Preserve is TRUE.
1 2 3 4 5 6 | Array ( [1] => 200 [2] => 50 [3] => 300 ) |
array_diff() Function
We use the
array_diff()
function to compare the first array in parameters with the rest of the arrays and returns an array that contains all the first array entries that are not present in any of the other arrays.
Syntax:
1 | array_diff(array1, array2) |
Example:
1 2 3 4 5 6 | <?php $array1 = array(11, 22, 33, 44); $array2 = array(11, 25, 33, 44); $diff = array_diff($array1, $array2); print_r($diff); ?> |
Output: It has returned 22 because it is not present in $array2.
1 2 3 4 | Array ( [1] => 22 ) |
array_search() Function
We use the
array_search()
function to search a value in an array and it returns the key.
Syntax:
1 | array_search(Value, Array, Strict) |
Value: This value to be search
Array: The value will be searched in this array.
Strict: It is a boolean parameter and the default value is FALSE. If you set it TRUE then it works as a strict comparison operator(===).
Example:
1 2 3 4 5 | <?php $array = array("a" => 11, "b" => 22, "c" => 33, "d" => 44); $searchResult = array_search(22, $array); echo $searchResult; ?> |
array_reverse() Function
We use the
array_reverse()
function to reverse the array.
Syntax:
1 | array_reverse(Array, Preserve) |
It takes two parameters and the first is required.
Array: This array will be reversed.
Preserve: It is a boolean parameter and the default value is FALSE. If you set it TRUE then it preserves the key values.
Example: In this example, take four elements of the array.
1 2 3 4 5 | <?php $array = array("a" => 11, "b" => 22, "c" => 33, "d" => 44); $returnArray = array_reverse($array); print_r($returnArray); ?> |
Output: The elements of the array have been reversed.
1 2 3 4 5 6 7 | Array ( [d] => 44 [c] => 33 [b] => 22 [a] => 11 ) |
Example:
1 2 3 4 5 | <?php $array = array(11, 22, 33, 44); $returnArray = array_reverse($array, TRUE); print_r($returnArray); ?> |
Output: The value of preserve argument is TRUE so the key has not reset.
1 2 3 4 5 6 7 | Array ( [3] => 44 [2] => 33 [1] => 22 [0] => 11 ) |
array_flip() Function
We use the
array_flip()
function to exchange the keys in an array with their corresponding values.
Syntax:
1 | array_flip(Array); |
Example:
1 2 3 4 5 | <?php $array = array('A' => 10, 'B' => 20, 'C' => 30); $flipValue = array_flip($array); print_r($flipValue); ?> |
Output: It has exchange the key and value.
1 2 3 4 5 6 | Array ( [10] => A [20] => B [30] => C ) |
array_replace() Function:
We use the
array_replace
function to replace the first array value from the next array values if the key is matched. If the key does not match then it adds the new elements.
Syntax:
1 | array_replace(Array, Array1, ...) |
It accepts a minimum of two parameters:
Array: The values of this array will be replaced.
Array1: If the key will match from the first array then the function will replace the value otherwise the function will add those elements to the first array.
Let’s take an example:
1 2 3 4 5 6 | <?php $array1 = array('A' => 10, 'B' => 20, 'C' => 30); $array2 = array('A' => 40, 'B' => 50, 'D' => 60); $arrayReplace = array_replace($array1, $array2); print_r($arrayReplace); ?> |
Output: In the above example, the key A and B are matching in both array so the value of the first array has been replaced by the value of the second array and it has returned a new array after adding new elements C and D.
1 2 3 4 5 6 7 | Array ( [A] => 40 [B] => 50 [C] => 30 [D] => 60 ) |
is_array() Function
We use the
is_array()
function to check whether a variable is an array or not. It returns TRUE or FALSE. You should use there where you do not know what kind of value will be set in this variable.
Syntax:
1 | is_array(Variable) |
It accepts a variable for checking.
Example:
1 2 3 4 5 6 7 8 | <?php $var = array('A' => 10, 'B' => 20, 'C' => 30); if(is_array($var)) { echo "This is an array variable."; } else { echo "This is not an array variable."; } ?> |
Output: The variable
$var
is an array so it is TRUE and output will be showing below.
1 | This is an array variable. |
Example:
1 2 3 4 5 6 7 8 | <?php $var = 80; if(is_array($var)) { echo "This is an array variable."; } else { echo "This is not an array variable."; } ?> |
Output: The variable $var is not an array so it will go in FALSE condition.
in_array() Function
We use the
in_array()
function to check the given value exists or not in an array. If the value exists then it will return TRUE otherwise it will return FALSE.
Syntax:
1 | in_array(Value, Array, Strict) |
Value: This value to search in the Array
Array: In this array, the value will be search
Strict: It is a boolean parameter and the default value is FALSE. If you set it TRUE then it works as a strict comparison operator(===).
Example:
1 2 3 4 5 6 7 8 | <?php $array = array(10,20,30,40); if(in_array(10, $array)) { echo "Matched"; } else { echo "Did not match"; } ?> |
Output:
1 | Matched |
We have covered the Top 16 Most Useful PHP Array Functions in this article. If you wish to share more information or have a query about the topic Top 16 Most Useful PHP Array Functions, please write in the comments box.