PHP Array Functions
PHP Array Functions array_pop
PHP provides a rich collection of built-in array functions that make it easy to work with, manipulate, and transform arrays. Whether you're working with indexed, associative, or multidimensional arrays, these functions help you perform tasks like sorting, merging, searching, slicing, filtering, and more.
This guide covers the most important PHP array functions with simple explanations and developer-friendly examples.
Returns the number of elements in an array.
$fruits = ["Apple", "Banana", "Orange"];
echo count($fruits); // 3
Adds one or more elements to the end of an array.
$colors = ["Red", "Green"];
array_push($colors, "Blue", "Yellow");
Removes and returns the last element of an array.
$numbers = [10, 20, 30];
$last = array_pop($numbers); // 30
Removes and returns the first element.
$names = ["Amit", "Ravi", "Sneha"];
$first = array_shift($names); // Amit
$names = ["Ravi", "Sneha"];
array_unshift($names, "Amit");
Useful for indexed arrays.
$fruits = ["Mango", "Apple", "Banana"];
sort($fruits);
$numbers = [50, 10, 30];
rsort($numbers);
$marks = ["Amit" => 80, "Sneha" => 90];
asort($marks);
$marks = ["Rahul" => 85, "Amit" => 90];
ksort($marks);
$colors = ["Red", "Blue", "Green"];
if (in_array("Blue", $colors)) echo "Found";
$user = ["name" => "Shubham", "city" => "Pune"];
if (array_key_exists("city", $user)) echo "Key exists";
$user = ["name" => "Amit", "age" => 25];
print_r(array_keys($user));
print_r(array_values($user));
$a = ["Red", "Blue"];
$b = ["Green", "Yellow"];
$merged = array_merge($a, $b);
$fruits = ["Apple", "Banana", "Orange", "Mango"];
print_r(array_slice($fruits, 1, 2)); // Banana, Orange
$fruits = ["Apple", "Banana", "Orange"];
array_splice($fruits, 1, 1, "Mango");
$fruits = ["a" => "Apple", "b" => "Banana"];
echo array_search("Banana", $fruits); // b
$numbers = [10, 20, 20, 30, 10];
print_r(array_unique($numbers));
$names = ["Amit", "Ravi", "Sneha"];
echo implode(", ", $names);
$data = "Amit,Ravi,Sneha";
print_r(explode(",", $data));
$numbers = [1, 2, 3];
$double = array_map(fn($n) => $n * 2, $numbers);
$numbers = [10, 25, 30, 40];
$filtered = array_filter($numbers, fn($n) => $n > 20);
$numbers = [1, 2, 3, 4];
$sum = array_reduce($numbers, fn($carry, $item) => $carry + $item, 0);
$cart = [
["item" => "Laptop", "price" => 55000],
["item" => "Mouse", "price" => 799]
];
$total = array_reduce($cart, fn($sum, $p) => $sum + $p['price'], 0);
echo "Total Price: ₹" . $total;
PHP offers dozens of array functions that make handling data easy, efficient, and powerful. Mastering these functions will help you write cleaner, faster, and more organized PHP code.