Topics Arrays & Collections Array Functions & Operations
intermediate 15 min read

Array Functions & Operations

Essential array functions for everyday PHP development.

Essential Array Functions

count()        // number of elements
array_push() // add to end
array_pop() // remove from end
array_shift() // remove from start
array_unshift() // add to start
in_array() // check if value exists
array_key_exists() / isset()
array_keys() // get all keys
array_values() // get all values
array_merge() // merge arrays
array_diff() // difference
array_intersect() // intersection
array_reverse() // reverse
array_unique() // remove duplicates
array_slice() // extract portion
array_chunk() // split into chunks
array_map() // apply callback
array_filter() // filter with callback
array_reduce() // reduce to single value
array_search() // find key by value
array_flip() // swap keys and values
array_combine() // keys from one, values from another

Examples

<?php
$a = [1, 2, 3, 4, 5];
$even = array_filter($a, fn($n) => $n % 2 === 0);
$squared = array_map(fn($n) => $n ** 2, $a);
print_r($even);
print_r($squared);

Your Notes

Sign in to take notes for this lesson.

Discussion

Sign in to join the discussion.

Flashcards

Sign in to create flashcards.