CGPA Calculator Hash Generator Bcrypt Hasher Tree Visualizer Barcode Generator Engineering Blog Editorial Standards All Tools Games HTML5 JavaScript PHP MySQL

PHP Array Functions

Written by RedoHub Editorial Team · Last updated: August 1, 2026

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.


1. Getting the Length

The count() function returns the number of elements in an array.

<?php
    $fruits = ["Apple", "Banana", "Cherry"];
    echo count($fruits); // Result: 3
?>

2. Adding and Removing Elements

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"]
?>

3. Merging Arrays

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"]
?>

4. Checking for Elements

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!";
    }
?>

5. Keys and Values

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"]
?>
Performance Tip: Built-in PHP functions are written in C and are much faster than writing your own logic using loops for basic tasks like searching or merging.
Caution: Functions like 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.

Commonly Used Array Functions

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.

Common Mistakes to Avoid

Assuming array_merge() always renumbers cleanly. It renumbers integer keys but keeps string keys — if two arrays share the same string key, the second array's value silently overwrites the first's.
Using 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.
Using 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.

Try It: Filtering and Transforming a Price List

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]
?>

Frequently Asked Questions

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.