MW

How-To · Backend

PHP Array Functions Reference (2026) — Manipulate, Filter, Sort, Transform

PHP has 80+ built-in array functions. These are the 20 you'll actually reach for in real code — with examples that run on PHP 8+.

Jan 24, 2025 6 min read Any platform beginner
Advertisement

PHP provides extensive array functions for manipulation, searching, filtering, sorting, and transformation. This guide covers the array functions you’ll actually use in production code — with examples that run on PHP 8+.

Terminal — PHP interactive shell (php -a)
$ php -a
Interactive shell php >
$ $a = [1, 2, 3]; print_r(array_map(fn($v) => $v * 2, $a));
Array ( [0] => 2 [1] => 4 [2] => 6 )

Introduction to PHP Array Functions

PHP arrays are powerful data structures, and PHP provides over 80 functions for working with them. Understanding these functions is essential for effective PHP development.

Array Function Categories:

  • Manipulation (add, remove, merge)
  • Searching (find, check existence)
  • Filtering (filter, extract)
  • Sorting (order, reverse)
  • Transformation (map, reduce, combine)

Key Concepts:

  • Indexed vs associative arrays
  • Key-value pairs
  • Array iteration
  • Functional programming patterns

Array Manipulation Functions

Adding Elements

array_push(): Add to end

$array = [1, 2, 3];
array_push($array, 4, 5);
// Result: [1, 2, 3, 4, 5]

array_unshift(): Add to beginning

$array = [2, 3];
array_unshift($array, 1);
// Result: [1, 2, 3]

Removing Elements

array_pop(): Remove from end

$array = [1, 2, 3];
$last = array_pop($array);
// $array: [1, 2], $last: 3

array_shift(): Remove from beginning

$array = [1, 2, 3];
$first = array_shift($array);
// $array: [2, 3], $first: 1

Merging Arrays

array_merge(): Combine arrays

$array1 = [1, 2];
$array2 = [3, 4];
$merged = array_merge($array1, $array2);
// Result: [1, 2, 3, 4]

Searching and Filtering

Searching

in_array(): Check if value exists

$array = [1, 2, 3];
$exists = in_array(2, $array); // true

array_search(): Find key by value

$array = ['a' => 1, 'b' => 2];
$key = array_search(2, $array); // 'b'

array_key_exists(): Check if key exists

$array = ['name' => 'John'];
$exists = array_key_exists('name', $array); // true

Filtering

array_filter(): Filter by callback

$numbers = [1, 2, 3, 4, 5];
$even = array_filter($numbers, fn($n) => $n % 2 === 0);
// Result: [1 => 2, 3 => 4]

array_slice(): Extract portion

$array = [1, 2, 3, 4, 5];
$slice = array_slice($array, 1, 3);
// Result: [2, 3, 4]

Sorting Functions

Basic Sorting

sort(): Sort by value (reindexes)

$array = [3, 1, 2];
sort($array);
// Result: [1, 2, 3]

rsort(): Reverse sort

$array = [1, 3, 2];
rsort($array);
// Result: [3, 2, 1]

Associative Sorting

asort(): Sort by value (preserves keys)

$array = ['b' => 2, 'a' => 1];
asort($array);
// Result: ['a' => 1, 'b' => 2]

ksort(): Sort by key

$array = ['b' => 2, 'a' => 1];
ksort($array);
// Result: ['a' => 1, 'b' => 2]

Transformation Functions

Mapping

array_map(): Apply function to each element

$numbers = [1, 2, 3];
$squared = array_map(fn($n) => $n * $n, $numbers);
// Result: [1, 4, 9]

Reducing

array_reduce(): Reduce to single value

$numbers = [1, 2, 3, 4];
$sum = array_reduce($numbers, fn($carry, $item) => $carry + $item, 0);
// Result: 10

Combining

array_combine(): Create associative array

$keys = ['a', 'b', 'c'];
$values = [1, 2, 3];
$combined = array_combine($keys, $values);
// Result: ['a' => 1, 'b' => 2, 'c' => 3]

Common Patterns

Extract Column

array_column(): Extract column from 2D array

$users = [
    ['id' => 1, 'name' => 'John'],
    ['id' => 2, 'name' => 'Jane'],
];
$names = array_column($users, 'name');
// Result: ['John', 'Jane']

Unique Values

array_unique(): Remove duplicates

$array = [1, 2, 2, 3];
$unique = array_unique($array);
// Result: [1, 2, 3]

Flip Array

array_flip(): Swap keys and values

$array = ['a' => 1, 'b' => 2];
$flipped = array_flip($array);
// Result: [1 => 'a', 2 => 'b']

Complete Function Reference

Manipulation: array_push, array_pop, array_shift, array_unshift, array_merge, array_splice Searching: in_array, array_search, array_key_exists, array_keys, array_values Filtering: array_filter, array_slice, array_chunk Sorting: sort, rsort, asort, arsort, ksort, krsort, usort Transformation: array_map, array_reduce, array_walk, array_combine Information: count, array_sum, array_product, array_keys, array_values

Conclusion

PHP array functions provide:

  • Comprehensive tools for array manipulation
  • Efficient operations for common tasks
  • Functional patterns for data transformation
  • Flexible options for different use cases

Key Points:

  • Learn core functions first
  • Combine functions for complex operations
  • Use appropriate function for task
  • Understand key preservation
  • Practice with real-world scenarios

Mastering PHP array functions is essential for effective PHP development and data manipulation.

PHP Arrays Functions Programming Reference
Advertisement

Get weekly notes in your inbox

Practical tips, tutorials and resources. No spam.