Useful PHP Array Functions Every Developer Should Know
PHP includes many array functions that make everyday data processing concise. Knowing a small set of reliable functions can reduce manual loops and make intent easier to understand.
Transform Values with array_map()
array_map() applies a callback to each value and returns a transformed array.
$prices = [10, 20, 30];
$withTax = array_map(
fn (int $price): float => $price * 1.2,
$prices
);
Filter Values with array_filter()
array_filter() keeps elements for which the callback returns true.
$numbers = [3, 8, 11, 14, 17];
$evenNumbers = array_filter(
$numbers,
fn (int $number): bool => $number % 2 === 0
);
By default, original keys are preserved. Use array_values() if a zero-based indexed result is required.
Search and Check
Use in_array() to check whether a value exists. Set its third argument to true for strict comparison. Use array_key_exists() to check for a key, including when its value is null.
$roles = ["editor", "author"];
if (in_array("editor", $roles, true)) {
echo "Access role found";
}
Merge and Reduce
array_merge() combines arrays, with later string keys replacing earlier matching keys. Numeric keys are appended and reindexed. array_reduce() processes values into a single result.
$prices = [12, 18, 25];
$total = array_reduce(
$prices,
fn (int $carry, int $price): int => $carry + $price,
0
);
Choose Functions Carefully
Read the function documentation for key behavior, callback arguments, and return values. For large datasets, consider whether processing everything in memory is appropriate. Database filtering and pagination may be more efficient.
Conclusion
Functions such as array_map(), array_filter(), in_array(), array_merge(), and array_reduce() cover many common tasks. Use them when they make data transformations clearer, not simply to make code shorter.