PHP Array Functions

PHP has more than 70 built-in functions to work with arrays. In this chapter you will learn the ones that you will use most, with short examples. If you are new to arrays, read PHP Arrays first.


Quick list

FunctionWhat it does
count()Number of values in an array
array_push(), array_pop()Add or remove a value at the end
array_unshift(), array_shift()Add or remove a value at the start
array_merge()Join arrays. See merge arrays
in_array(), array_search()Check or find a value
array_keys(), array_values()Get all keys or all values. See keys and values
array_slice()Take a part of an array
array_unique()Remove duplicate values. See remove duplicates
array_reverse()Reverse the order
array_map(), array_filter(), array_reduce()Change, select or combine values using a function
array_column()Get one column from an array of arrays
sort(), rsort(), asort(), ksort()Sort an array. See ksort()

Sort an array

<?php
$numbers = [5, 3, 8, 1];

sort($numbers);
print_r($numbers);
?>

Output:

Tutorials Class - Output Window

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

FunctionSorts
sort() / rsort()By value, low to high / high to low (new keys)
asort() / arsort()By value, keeps the keys
ksort() / krsort()By key, low to high / high to low

array_map(), array_filter() and array_reduce()

These three functions take another function as a parameter. Here we use short arrow functions.

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

// array_map: change every value
print_r(array_map(fn($n) => $n * 2, [1, 2, 3]));

// array_filter: keep the values where the function gives true
print_r(array_filter($numbers, fn($n) => $n % 2 == 0));

// array_reduce: join all values in one value
echo array_reduce([1, 2, 3, 4], fn($total, $n) => $total + $n, 0);
?>

Output:

Tutorials Class - Output Window

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

Notice that array_filter() keeps the original keys (1, 3, 5).


Search in an array

<?php
$fruits = ["Apple", "Banana", "Cherry"];

var_dump(in_array("Banana", $fruits));
echo array_search("Cherry", $fruits);
?>

Output:

Tutorials Class - Output Window

bool(true)
2


array_slice() and array_column()

<?php
print_r(array_slice([10, 20, 30, 40, 50], 1, 3));

$users = [
    ["id" => 1, "name" => "Ram"],
    ["id" => 2, "name" => "Sanjay"]
];

print_r(array_column($users, "name"));
?>

Output:

Tutorials Class - Output Window

Array
(
[0] => 20
[1] => 30
[2] => 40
)
Array
(
[0] => Ram
[1] => Sanjay
)


Add and remove values

<?php
$stack = [1, 2];

array_push($stack, 3);   // add 3 at the end
echo array_pop($stack);  // remove and show the last value
echo "<br>";
echo count($stack);
?>

Output:

Tutorials Class - Output Window

3
2

Note: Many array functions change the original array (like sort), and many return a new one (like array_map). Check the manual on php.net if you are not sure. Practice with the PHP array interview questions.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
Remove specific element by value from an array in PHP?
PHP Array to String Conversion (favourite colours chosen by user)
How to check if an array is a subset of another in PHP?
Interview Questions & Answers
What are the different PHP functions to sort an array?
PHP’s numerically indexed array begins with which position?
Which PHP function inserts an element to the end of an array?
Which PHP function can be used check if variable is an array?
How many types of arrays are available in PHP?
What is the use of array_chunk() function in PHP?
What is the use of in_array() function in PHP?