Coding/Learn/php/php arrays and array functions
PHP Foundational Course
01.Introduction, Server-Side Architecture & Syntax
02.Variables, Strict Types & Dynamic Casting
03.Strings, Numbers & Built-in Math Functions
04.Operators, Null Coalescing & Magic Constants
05.Control Flow: If-Else, Switch & Modern Match Expressions
06.Loops: While, For & Foreach Traversal
07.Functions, Type Declarations & Named Arguments
08.Arrays & Functional Array Manipulation
09.Superglobals, Form Handling & Input Sanitization
10.Date, Time & DateTimeImmutable Objects
11.File System, Streams & I/O Operations
12.Cookies, Sessions & State Management
13.Exceptions, Throwables & Custom Error Fences
14.Classes, Objects & Constructor Property Promotion
15.Inheritance, Access Modifiers & Method Overriding
16.Interfaces, Abstract Classes & Polymorphism
17.Traits, Static Members & Namespaces
18.PDO, MySQL & Prepared Statements against SQL Injection
19.JSON & XML REST API Interoperability
20.Capstone: Production Secure Backend API & User Registry Engine
Course Certificate0%

Complete all lessons to earn

LearnArrays & Functional Array Manipulation
8 / 20 0%
Lesson 08 of 20
10 mins read

Arrays & Functional Array Manipulation

PreviousNext Lesson
In Plain English

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).

The Mental Model

"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.

PointCore Rules to Remember

Point star
Ordered Hash Map: PHP arrays preserve insertion order for both indexed and string-keyed elements.
Point star
Array Functions Suite: PHP includes over 80 built-in array functions for sorting, searching, and transforming data.
Point star
Spread Unpacking: Use ... to unpack arrays into function calls or merge arrays with precedence.

Common Beginner Traps & How to Fix Them

Mistake: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.

Pro Tip: Use array_key_exists() when checking if a key exists with a null value, whereas isset() returns false for null values.

Live Interactive Example

Hit Run Code to see it live
Functional Pipeline: Filter, Map and Sum
PHP 8.2 / CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Output Console
Click "Run Code" to view the rendered output.
How it works: Filtered down to 2 in-stock items ($100 + $250) and computed exact sum of 350.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Double and Filter Numbers

  • PointDeclare $nums = [5, 12, 8, 20, 3].
  • PointFilter numbers that are greater than 6.
  • PointMultiply each remaining number by 2 using array_map.
  • PointPrint the resulting comma-separated list using implode(", ", $result).
1
2
3
4
5
6
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates