Arrays in PHP
Master indexed, associative, and multidimensional arrays in PHP, along with common array functions for sorting, filtering, and transforming data.
Array Creation
PHP arrays are extremely versatile — a single `array` type covers indexed lists, key-value maps (associative arrays), and any mix of both. You create them using the short `[]` syntax (PHP 5.4+) or the older `array()` function.
1. Indexed Array
An ordered list of values, accessed by numeric index starting at 0.
2. Associative Array
A key-value map where keys are strings (or integers).
3. Mixed Array
PHP arrays can mix integer and string keys in one array.
Array Fundamentals
The core operations on an array: reading its size, accessing an element by index or key, updating a value, and removing an element.
1. Getting Array Size
`count()` returns the number of elements.
2. Accessing an Element
Use `$arr[index]` for indexed or `$arr["key"]` for associative.
3. Updating an Element
Assign a new value to an existing key.
4. Removing an Element
Use `unset()` to remove an element by key.
Traversing an Array
PHP provides several ways to iterate over arrays. `foreach` is the idiomatic choice for both indexed and associative arrays.
1. for Loop (Indexed Arrays)
Use when you need the numeric index during iteration.
2. foreach (Indexed & Associative)
The idiomatic way to iterate over any PHP array.
3. array_walk() — forEach with Callback
Apply a callback to every element in-place.
Searching, Joining & Comparing
PHP provides dedicated functions to check for element existence, find indices, join elements into a string, and compare two arrays.
1. Check if an Element Exists — in_array()
Returns true if a value exists in the array.
2. Find Element Index — array_search()
Returns the key of the first matching value, or false.
3. Check if a Key Exists — array_key_exists()
Check whether a specific key exists in an associative array.
4. Joining Array Elements — implode()
Join all elements into a single string with a separator.
5. Comparing Arrays
Use == for loose comparison and === for strict (values + types + order).
Insertion & Removal
PHP provides functions to push and pop from the end, shift and unshift from the start, and splice at any position.
1. Insert / Remove from End
`[]` or `array_push()` to add; `array_pop()` to remove.
2. Insert / Remove from Start
`array_unshift()` to add at front; `array_shift()` to remove from front.
3. Insert & Remove at Specific Index — array_splice()
Remove and/or insert elements at any position.
Advanced Array Operations
PHP's standard library includes a rich set of array functions for reversing, sorting, deduplicating, flattening, and cloning arrays.
1. Clear an Array
Remove all elements from an array without destroying the variable.
3. Reverse an Array
`array_reverse()` returns a reversed copy.
4. Sort an Array
PHP has separate sort functions for values, keys, and custom comparison.
5. Shuffle an Array
`shuffle()` randomizes the order of elements in place.
6. Multidimensional Arrays
Arrays of arrays for representing tables and nested data.
7. Concatenating Arrays — array_merge()
Combine two or more arrays into one.
8. Flattening Arrays — array_merge() + spread
Collapse a nested array one level deep.
9. Removing Duplicates — array_unique()
Return an array with duplicate values removed.
10. Cloning Arrays
PHP arrays are value types — assignment copies them automatically.
Higher-Order Array Functions
PHP's built-in array functions like `array_map`, `array_filter`, and `array_reduce` are higher-order functions — they accept a callback that defines the transformation logic.
1. array_map() — Transform Every Element
Apply a callback to each element and return a new array.
2. array_filter() — Keep Matching Elements
Return only elements where the callback returns true.
3. array_reduce() — Accumulate to a Single Value
Fold an array into one value using a callback.
4. Finding Elements — array_find() / array_filter()
Find the first element matching a condition.
5. any / all — array_any() / array_all() — PHP 8.4+
Check if any or all elements satisfy a condition.
6. Chaining Array Operations
Combine map, filter, and reduce in a pipeline.