How to access first level keys of a 2d array with a foreach loop?
arrays, foreach, key, multidimensional-array, php
Solution
You can access your array keys like so:
foreach ($array as $key => $value)
Problem
How do I access the first level key of a two-dimensional array using a foreach loop? I have a `$places` array like this: ``` [Philadelphia] => Array ( [0] => Array ( [place_name] => XYX [place_id] => 103200 [place_status] => 0 ) [1] => Array ( [place_name] => YYYY [place_id] => 232323 [place_status] => 0 ) ``` This is my view code that loops over the array: ``` <?php foreach($places as $site): ?> <h5><?=key($site)?></h5> <?php foreach($site as $place): ?> <h6><?=$place['place_name']?></h6> <?php endforeach?> <?php endforeach ?> ``` Where I call `key($site)`, I want to get `Philadelphia`, but I am just getting `place_name` from each row.