PHP array functions

5/5 - (1 vote)

List of functions that work with php arrays.


Checks if the given key or index exists in the array. array_key_exists() returns TRUE if the given key is set in the array.

or you can use isset().

null coalescing operator (??)

PHP 7 introduced a new feature, null coalescing operator (??).
It is used to replace the ternary operation in conjunction with isset() function. The Null coalescing operator returns its first operand if it exists and is not NULL; otherwise it returns its second operand


array_shift() – Shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won’t be touched.

$numbers = [1,2,3,4,5,6];

    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 6


array_pop() – pop the last element out of an array

$numbers = [1,2,3,4,5,6];

    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5

!The difference between array_shift() and array_pop() is that the array_shift() drag the first element from array while array_pop() drag the last element.


array_unshift() — Add one or more elements to the beginning of an array

$colors = ['red','white','blue'];

    [0] => black
    [1] => red
    [2] => white
    [3] => blue


array_push() – Push one or more elements onto the end of array

$colors = ['red','white','blue'];

    [0] => red
    [1] => white
    [2] => blue
    [3] => black

!The difference between array_unshift() and array_push() is that the array_unshift() add the first element from array while array_push() add the last element.


count() – returns the number of elements as an integer.

$numbers = [1,2,3,4,5,6];
echo count($numbers);
// result: 6

Attention! For counting all the elements of a multidimensional array, we need to use the optional mode parameter COUNT_RECURSIVE.

$cities = array("France"=>array("Domme","Paris", "Cannes"), "Uk"=>array("London", "Newport", "Manchester"));
echo count($cities);
//Result: 2

echo count($cities,COUNT_RECURSIVE);
//Result: 8


sort() – sorts an array from lowest to highest values. If two values as equal, their relative order in the sorted array is undefined.

SORT_REGULAR – compare items normally (don’t change types)
SORT_NUMERIC – compare items numerically
SORT_STRING – compare items as strings

$number = [7,5,2,1,1,0,6];

    [0] => 0
    [1] => 1
    [2] => 1
    [3] => 2
    [4] => 5
    [5] => 6
    [6] => 7


asort() – Sort an array and maintain index association. Asort() is used mainly when sorting associative arrays where the actual element order is significant.

$numbers = array(8,5,8,1,2,4,0);

    [6] => 0
    [3] => 1
    [4] => 2
    [5] => 4
    [1] => 5
    [0] => 8
    [2] => 8


array_slice() — Extract a slice of the array.

To use it, pass it the array to extract the slice from, followed by the position of the first element (counting from zero), followed by the number of elements to extract.

Attention! The function returns a new array containing copies of the elements you extracted (it doesn’t touch the original array).

$cities = ["London", "Paris", "Roma", "Praga", "Berlin"];

    [0] => Paris
    [1] => Roma
    [2] => Praga

You can use array_slice() with associative arrays to.

If the third argument to array_slice() is missing the function extracts all elements from the start position to the end of the array.


in_array() checks if a value exists in an array. Returns TRUE if the element is found in the array, FALSE otherwise.

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

Also, there is a third parameter, default set as false (bool $strict = FALSE). If the third parameter $strict is set to TRUE then in_array() will also check the types of the element.

$numbers = [7,9,1,1,6,5];

if(in_array("1", $numbers)){
    echo "1. The element is in array";

//strict is set to TRUE

if(in_array("1", $numbers, TRUE)){
    echo "2. The element is in array";

//1. The element is in array


implode() return a string containing all the array elements in the same order.

$colors = array('red','white','blue');
$all_colors = implode(" * ",$colors);
echo $all_colors;

//red * white * blue


explode() breaks a string into an array. The “separator” parameter cannot be an empty string.

$all_colors = "red * white * blue";
$colors = explode(" * ",$all_colors);

//Array ( [0] => red [1] => white [2] => blue ) 


The shuffle() function is used to rearrange an array in random order.

Php array functions interview questions and answers

What function is used to sort the array in alphabetical order of the keys?


What function is used to rearrange an array in random order?


What function is used to sort the values in array and keep the keys intact?


Hello there!

I hope you find this post useful!

I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

subscribe youtube

Leave a Comment