Wikis & learning

Walk a nested collection

A list of associative arrays can represent the rows of a small catalog. Each row can use the same keys while holding different values.

A list of associative arrays can represent the rows of a small catalog. Each row can use the same keys while holding different values.

How it works

Select the row first, then read its named field. Use a loop when the number of rows can change.

PHP example

<?php
$tools = [
    ['name' => 'Formatter', 'kind' => 'CLI'],
    ['name' => 'Preview', 'kind' => 'Web'],
];
foreach ($tools as $tool) {
    echo $tool['name'] . ' (' . $tool['kind'] . ')' . PHP_EOL;
}

Expected result

Two lines, Formatter (CLI) and Preview (Web).

Try a variation

Append a third tool. Its row should appear without changing the loop.