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. Useisset()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, useusort()with a callback.
📸 Verified Output:
Step 3: array_map, array_filter, array_reduce
💡
array_filterpreserves original keys — after filtering, keys may be non-contiguous (e.g., 1, 3, 5). Usearray_values()to reindex if you need 0-based sequential keys.array_mapwith 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 ofSELECT name FROM students. Combined witharray_sum()andcount(), it replaces many database queries during local data processing.
📸 Verified Output:
Step 5: Array Merging, Combining & Set Operations
💡
array_mergevs+operator:array_mergereindexes numeric keys (combines both);+keeps the first array's value for duplicate keys (union). For config arrays where you want "default + override", usearray_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()andextract()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_sumis the PHP idiom for summing a field across all rows — equivalent toSELECT SUM(subtotal) FROM cart. Thematchexpression (PHP 8.0+) is a strict, exhaustive switch — it throwsUnhandledMatchErrorif no arm matches (unlikeswitchwhich 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
