PHP has a vast set of built-in functions that allow you to modify, search, and manage arrays efficiently. Understanding these functions is key to professional PHP development.
The count() function returns the number of elements in an array.
<?php
$fruits = ["Apple", "Banana", "Cherry"];
echo count($fruits); // Result: 3
?>
You can easily add or remove elements from the beginning or end of an array.
array_push(): Adds one or more elements to the end.array_pop(): Removes the last element.array_unshift(): Adds one or more elements to the beginning.array_shift(): Removes the first element.<?php
$stack = ["Orange"];
array_push($stack, "Apple", "Mango"); // ["Orange", "Apple", "Mango"]
array_pop($stack); // ["Orange", "Apple"]
?>
The array_merge() function merges one or more arrays into one.
<?php
$a1 = ["red", "green"];
$a2 = ["blue", "yellow"];
$result = array_merge($a1, $a2);
// Result: ["red", "green", "blue", "yellow"]
?>
The in_array() function checks if a specific value exists in an array.
<?php
$os = ["Windows", "Linux", "macOS"];
if (in_array("Linux", $os)) {
echo "Found Linux!";
}
?>
For associative arrays, you might need just the keys or just the values.
array_keys(): Returns all the keys.array_values(): Returns all the values.<?php
$age = ["Peter"=>"35", "Ben"=>"37", "Joe"=>"43"];
print_r(array_keys($age)); // ["Peter", "Ben", "Joe"]
?>
array_merge() will overwrite numeric keys if they collide, but they will append if the keys are integers. Always check the manual for specific behavior.
| Function | Description |
|---|---|
count() | Returns the number of elements. |
array_push() | Inserts elements at the end. |
array_merge() | Merges two or more arrays. |
in_array() | Checks if a value exists. |
array_slice() | Extracts a slice of the array. |
array_unique() | Removes duplicate values. |
in_array() without strict mode. By default it does a loose comparison, so in_array(0, ["Apple", "Banana"]) can return unexpected results with certain PHP versions. Pass true as the third argument for strict comparison.
array_push() to add one element. $arr[] = $value; does the same thing with less function-call overhead — reserve array_push() for adding multiple elements at once.
A second example combining two functions not shown above — filtering out cheap items, then applying a 10% discount to what's left:
<?php
$prices = [15, 120, 45, 200, 8];
$expensive = array_filter($prices, fn($p) => $p > 50);
$discounted = array_map(fn($p) => $p * 0.9, $expensive);
print_r($discounted); // [1 => 108, 3 => 180]
?>
Q: What's the difference between array_push() and $arr[] = ?
A: They do the same thing for a single element, but $arr[] = $value; is slightly faster since it skips a function call — array_push() is more useful when adding several elements at once.
Q: Does in_array() do a strict comparison by default?
A: No — pass true as the third argument (in_array($needle, $haystack, true)) if you need type-strict matching.
Q: How do I remove duplicate values from an array?
A: Use array_unique($array). See the full PHP array functions reference for the complete list of built-ins.