Python Arrays & Functional Array Manipulation
PHP arrays are versatile ordered maps that function as lists, stacks, queues, and hash tables. Master short array syntax ([]), associative key-value mapping, array destructuring, and functional array transformers (array_map, array_filter, array_reduce, in_array).
"A PHP array is like a Swiss Army knife: it acts as a simple sequential list of items, an indexed encyclopedia where words point to definitions, or a nested hierarchy of folders."
Deep Dive: How It Works
Indexed vs Associative: $list = ["apple", "banana"] vs $dict = ["name" => "Alice", "role" => "Admin"].
Array Spread Operator: $combined = [...$arr1, ...$arr2] merges arrays efficiently.
Destructuring: [$first, $second] = $arr or ["name" => $userName] = $data.
Functional Helpers: array_map() transforms values, array_filter() removes falsy items, array_reduce() accumulates a single aggregate.
Syntax Blueprint
<?php
$evens = array_filter($nums, fn($n) => $n % 2 === 0);
$names = array_column($users, "name");
$hasKey = array_key_exists("id", $record);Transform collections with functional array functions and extract nested columns with array_column.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
array_filter preserves original integer indices by default, which serializes to JSON as an object instead of an array.Why it happens: array_filter retains keys 0, 2, 4 if elements 1 and 3 are removed.
How to fix: Wrap result in array_values($filtered) to re-index the array from 0.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Double and Filter Numbers
Declare $nums = [5, 12, 8, 20, 3].
Filter numbers that are greater than 6.
Multiply each remaining number by 2 using array_map.
Print the resulting comma-separated list using implode(", ", $result).
Finished reading and practicing?
Mark this lesson as completed to update your course progress.