Originally published on zero2php.dev — part of the Zero to PHP course. View this lesson on the original site.
Ordered maps
PHP arrays are ordered maps—list-like (0..n) or associative (key => value) or mixed.
Example
<?php
declare(strict_types=1);
$nums = [10, 20, 30];
$user = ['name' => 'Ada', 'role' => 'dev'];
echo $nums[1], "\n";
echo $user['name'], "\n";
Iteration
foreach is the default iteration tool; index loops are for when you need positions.
Exercise
Given $items = ['x', 'y', 'z'], echo the element at index 1 (single character y).
From Zero to PHP — Control flow, functions, arrays, and the web