Top 25+ PHP Array Functions with Examples & Output (2025)

Arrays are a fundamental data structure in PHP, used extensively for storing and manipulating data. Whether you’re building a dynamic web application or handling API responses, PHP array functions allow you to work more efficiently. In this SEO-optimized guide, we’ll walk through the most useful array functions in PHP—with practical examples, expected outputs, and real-world use cases.

  • 1. Stack & Queue Functions

    array_push()

    Adds one or more elements to the end of an array.

    $fruits = ['apple', 'banana'];
    array_push($fruits, 'orange', 'mango');
    print_r($fruits);

    Output

    Array ( [0] => apple [1] => banana [2] => orange [3] => mango )

    array_pop()

    Removes the last element of the array and returns it.

    $colors = ['red', 'green', 'blue'];
    $last = array_pop($colors);
    echo $last;

    Output:

    blue

    array_unshift()

    Adds elements to the beginning of an array.

    $queue = ['user2', 'user3'];
    array_unshift($queue, 'user1');
    print_r($queue);

    Output:

    Array ( [0] => user1 [1] => user2 [2] => user3 )

    array_shift()

    Removes the first element and shifts the rest of the array.

    $queue = ['first', 'second', 'third'];
    $removed = array_shift($queue);
    echo $removed;

    Output:

    first
  • 2. Transforming Arrays

    array_map()

    Applies a callback to each element.

    $numbers = [1, 2, 3];
    $squares = array_map(fn($n) => $n * $n, $numbers);
    print_r($squares);

    Output:

    Array ( [0] => 1 [1] => 4 [2] => 9 )

    array_filter()

    Filters elements using a callback.

    $ages = [22, 17, 30];
    $adults = array_filter($ages, fn($age) => $age >= 18);
    print_r($adults);

    Output:

    Array ( [0] => 22 [2] => 30 )
  • 3. Aggregating Arrays

    array_reduce()

    Reduces array to a single value via callback.

    $items = [10, 20, 30];
    $total = array_reduce($items, fn($carry, $item) => $carry + $item, 0);
    echo $total;

    Output:

  • 4. Inspecting Keys & Values

    array_keys()

    $data = ['name' => 'John', 'age' => 30];
    $keys = array_keys($data);
    print_r($keys);
    

    Output:

    Array ( [0] => name [1] => age )

    array_values()

    Returns all values in the array.

    $info = ['a' => 100, 'b' => 200];
    print_r(array_values($info));

    Output:

    Array ( [0] => 100 [1] => 200 )

    in_array()

    Checks if value exists in array.

    $roles = ['admin', 'editor', 'subscriber'];
    echo in_array('editor', $roles) ? 'Found' : 'Not Found';

    Output:

    Found

    array_key_exists()

    Checks if a key exists in array.

    $profile = ['name' => 'Alex'];
    echo array_key_exists('name', $profile) ? 'Yes' : 'No';

    Output:

    Yes
  • 5. Merging & Comparing Arrays

    array_merge()

    Merges arrays, overriding keys if needed.

    $a = ['color' => 'red'];
    $b = ['size' => 'M'];
    print_r(array_merge($a, $b));

    Output:

    Array ( [color] => red [size] => M )

    array_unique()

    Removes duplicate values.

    $nums = [1, 2, 2, 3];
    print_r(array_unique($nums));

    Output:

    Array ( [0] => 1 [1] => 2 [3] => 3 )

    array_diff()

    Finds values in first array not present in others.

    $a = [1, 2, 3, 4];
    $b = [3, 4, 5];
    print_r(array_diff($a, $b));

    Output:

    Array ( [0] => 1 [1] => 2 )

    array_intersect()

    Returns common values.

    $a = ['a', 'b', 'c'];
    $b = ['b', 'c', 'd'];
    print_r(array_intersect($a, $b));

    Output:

    Array ( [1] => b [2] => c )
  • 6. Extracting and Slicing

    array_slice()

    Extracts a portion of an array.

    $data = ['a', 'b', 'c', 'd'];
    print_r(array_slice($data, 1, 2));

    Output:

    Array ( [0] => b [1] => c )

    array_column()

    Extracts column from multidimensional array.

    $records = [
      ['id' => 1, 'name' => 'John'],
      ['id' => 2, 'name' => 'Jane']
    ];
    print_r(array_column($records, 'name'));

    Output:

    Array ( [0] => John [1] => Jane )
  • 7. Searching & Flipping

    array_search()

    Searches for a value and returns its key.

    $items = ['pen' => 10, 'pencil' => 5];
    echo array_search(10, $items);

    Output:

    pen

    array_flip()

    Swaps keys with values.

    $flip = ['a' => 1, 'b' => 2];
    print_r(array_flip($flip));

    Output:

    Array ( [1] => a [2] => b )
  • 8. Sorting Arrays

    sort()

    Sorts array in ascending order (reindexing keys).

    $nums = [3, 1, 2];
    sort($nums);
    print_r($nums);

    Output:

    Array ( [0] => 1 [1] => 2 [2] => 3 )

    rsort()

    Sorts array in descending order.

    rsort($nums);
    print_r($nums);

    Output:

    Array ( [0] => 3 [1] => 2 [2] => 1 )

    asort()

    Sorts while maintaining index association.

    $data = ['a' => 3, 'b' => 1];
    asort($data);
    print_r($data);

    Output:

    Array ( [b] => 1 [a] => 3 )

    ksort()

    Sorts by key.

    $data = ['b' => 2, 'a' => 1];
    ksort($data);
    print_r($data);
    

    Output:

    Array ( [a] => 1 [b] => 2 )

    natcasesort()

    Natural order sort, case-insensitive.

    $items = ['img1', 'Img10', 'img2'];
    natcasesort($items);
    print_r($items);

    Output:

    Array ( [0] => img1 [2] => img2 [1] => Img10 )
  • 9. Random Access

    shuffle()

    $cards = ['A', 'K', 'Q'];
    shuffle($cards);
    print_r($cards);

    Output: Random every time

    array_rand()

    Picks one or more random keys.

    $colors = ['red', 'green', 'blue'];
    $key = array_rand($colors);
    echo $colors[$key];

    Output: Random every time

  • 10. Custom Sorting

    usort()

    Custom comparison for sorting.

    $nums = [4, 2, 8, 6];
    usort($nums, fn($a, $b) => $a <=> $b);
    print_r($nums);

    Output:

    Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 )

Also explore our comprehensive article on PHP string functions for better handling of text data in PHP.

Mastering these array functions is critical for clean, efficient PHP code. Using functions like array_map, array_filter, and array_merge can reduce lines of code and make your app logic more readable.