Lab 03: Arrays

Objective

Create and manipulate PHP arrays — indexed, associative, and multidimensional — using built-in array functions for sorting, filtering, mapping, and transformation.

Background

PHP arrays are one of the most versatile data structures in any language — they function as lists, dictionaries, stacks, queues, and sets all in one. PHP has over 70 built-in array functions. Mastering them is essential for web development, API responses, database result processing, and configuration management.

Time

35 minutes

Prerequisites

  • Lab 02 (Data Types)

Tools

  • PHP 8.3 CLI

  • Docker image: zchencow/innozverse-php:latest


Lab Instructions

Step 1: Indexed and Associative Arrays

💡 PHP arrays are ordered maps — even indexed arrays remember insertion order. array_key_exists() checks if a key exists (even if null); isset() checks if it exists AND is not null. Use isset() for performance-critical code; it's a language construct, not a function.

📸 Verified Output:


Step 2: Array Functions — Sort, Search, Slice

💡 sort() reindexes the array — keys are reset to 0, 1, 2… asort() preserves key-value associations while sorting by value. ksort() sorts by key. For custom sort order, use usort() with a callback.

📸 Verified Output:


Step 3: array_map, array_filter, array_reduce

💡 array_filter preserves original keys — after filtering, keys may be non-contiguous (e.g., 1, 3, 5). Use array_values() to reindex if you need 0-based sequential keys. array_map with multiple arrays applies the callback to corresponding elements.

📸 Verified Output:


Step 4: Multidimensional Arrays

💡 array_column($array, $key) extracts a single column from a 2D array — it's the PHP equivalent of SELECT name FROM students. Combined with array_sum() and count(), it replaces many database queries during local data processing.

📸 Verified Output:


Step 5: Array Merging, Combining & Set Operations

💡 array_merge vs + operator: array_merge reindexes numeric keys (combines both); + keeps the first array's value for duplicate keys (union). For config arrays where you want "default + override", use array_merge($defaults, $overrides).

📸 Verified Output:


Step 6: Array Unpacking & Spread

💡 Array destructuring with [...] (PHP 7.1+) lets you unpack arrays into variables elegantly. The spread operator ... works for both function arguments and array literals. These patterns make coordinate unpacking, config loading, and function calls much cleaner.

📸 Verified Output:


Step 7: Useful Array Utilities

💡 compact() and extract() are PHP-unique functions. compact() builds an array from variable names — useful for passing multiple vars to templates. extract() does the reverse — use it carefully as it can overwrite existing variables. Prefer explicit array access in most cases.

📸 Verified Output:


Step 8: Real-World — Shopping Cart

💡 array_column + array_sum is the PHP idiom for summing a field across all rows — equivalent to SELECT SUM(subtotal) FROM cart. The match expression (PHP 8.0+) is a strict, exhaustive switch — it throws UnhandledMatchError if no arm matches (unlike switch which falls through silently).

📸 Verified Output:


Verification

Expected: Sum of even squares: 220

Summary

PHP arrays are maps, lists, stacks, queues, and sets — all in one. You've covered indexed and associative arrays, all major array functions (sort, filter, map, reduce, column, chunk, merge), multidimensional arrays, destructuring, and built a real shopping cart. Array mastery is the foundation of PHP backend work.

Further Reading

Last updated