PHP Array Functions (with Output)
In this lesson: Most-used PHP array functions — sort, usort, array_map, array_filter, array_merge, in_array — with output.
Why array functions
PHP has dozens of built-in array functions that do common tasks — sorting, searching, transforming, combining — in one line, so you rarely need to write loops for them. Learning a handful of the most useful ones makes your code shorter and clearer.
sort); others return a new array (like array_map). Know which is which.count, sort, reverse
<?php
$n = [3, 1, 2];
echo count($n); // 3
sort($n); // [1, 2, 3]
print_r($n);
$r = array_reverse($n); // [3, 2, 1]
?>
Array ( [0] => 1 [1] => 2 [2] => 3 )
count gives the size, sort orders values (changing the array in place), and array_reverse returns a reversed copy.
in_array and array_search
<?php
$fruits = ["apple", "mango", "banana"];
var_dump(in_array("mango", $fruits)); // true
echo array_search("banana", $fruits); // 2 (its index)
?>
2
in_array tells you if a value exists (true/false); array_search returns its key/index, or false if not found.
array_map
Apply a function to every item and get a new array back.
<?php
$nums = [1, 2, 3];
$doubled = array_map(function($x) { return $x * 2; }, $nums);
print_r($doubled);
?>
Every number is doubled. The original $nums stays the same — map returns a fresh array of results.
array_filter
Keep only items that pass a test.
<?php
$nums = [1, 2, 3, 4, 5, 6];
$even = array_filter($nums, function($x) { return $x % 2 == 0; });
print_r($even);
?>
Only even numbers remain. Note the keys stay from the original — use array_values() if you want them renumbered from 0.
array_merge and array_keys
<?php
$a = ["x" => 1, "y" => 2];
$b = ["y" => 9, "z" => 3];
print_r(array_merge($a, $b)); // y becomes 9
print_r(array_keys($a)); // ["x", "y"]
?>
Array ( [0] => x [1] => y )
array_merge combines arrays (later string keys overwrite earlier ones); array_keys returns all the keys.
Common mistakes
- Expecting sort to return a value.
sort()changes the array in place and returns true/false, not the sorted array. - Forgetting filter keeps original keys. Use
array_values()to renumber if needed. - Checking in_array with loose match. Pass
trueas the third argument for a strict (type-aware) check. - Assuming array_map changes the original. It returns a new array; the original is untouched.
Summary
count,sort,array_reversefor size and order.in_arrayandarray_searchto find values.array_maptransforms every item into a new array.array_filterkeeps items that pass a test.array_mergecombines arrays;array_keyslists keys.
Next, review the basics in PHP arrays, or loop over them with PHP loops.
Frequently Asked Questions
What does array_map do in PHP?
array_map applies a function to every item of an array and returns a new array of the results. For example, doubling every number or trimming every string. The original array is not changed.What is the difference between array_map and array_filter?
array_map transforms every item and keeps the same count. array_filter keeps only the items that pass a test and can return fewer items. Map changes values; filter selects values.How do I sort an array in PHP?
sort() for values in ascending order, rsort() for descending, and asort()/ksort() to sort associative arrays by value or key. Most sort functions change the array in place.How do I check if a value exists in an array?
in_array("x", $arr) which returns true or false, or array_search("x", $arr) which returns the key/index of the value (or false if not found).Does array_merge keep keys?
array_merge re-indexes from 0. For string keys, later values overwrite earlier ones with the same key. Knowing this avoids surprises when combining arrays.