Array Traversal

In PHP it's easy to get JSON decoded by $data = json_decode($input). But many newcomers are confused by how to pick individual information from the resulting array or object structure then. This visualization is intended to help.

Hover with the mouse over entries or keys to see the required array or object traversal syntax:

$data

Array(
[0] =>
Array(
[capital] => Sacramento
[joined_union] => 1850
[population_rank] => 1
)
[1] =>
Array(
[capital] => Austin
[joined_union] => 1845
[population_rank] => 2
)
[2] =>
Array(
[capital] => Boston
[joined_union] => 1788
[population_rank] => 14
)
)

How to loop over this structure

foreach ($data as $i=>$value) {
     print $value['capital'];
}

Note that this auto-generated loop might only work if the array/object structure is very uniform (no discrepancies in subarray schemes). It also might skip consecutive indexed array levels.


link

Also check the manual on array syntax and how to work with objects.
Or how to use a foreach to loop over entries.