Skip to content
Advertisement

Return PHP array name

Array ( [kanye] => Array ( [0] => Kanya [1] => Janaye [2] => Kayne [3] => Kane [4] => Kaye ) [wst] => Array ( [0] => ST [1] => St [2] => st [3] => EST [4] => West ) )

Array
(
    [0] => Kanya
    [1] => Janaye
    [2] => Kayne
    [3] => Kane
    [4] => Kaye
)
Array
(
    [0] => ST
    [1] => St
    [2] => st
    [3] => EST
    [4] => West
)

I’ve got those two arrays inside one array. The top array holds them both, then below is each one individually. When I am displaying the individual arrays, how do I echo their name?

So the first would be kanye, then list the contents, etc.

Hope that makes sense. I know it will be a simple bit of code but it’s stumping me.

Advertisement

Answer

You can use a foreach statement to get the key value pair of the array:

$outer_arr = array('kanye' => array('Kanya', 'Janaye', 'Kayne', 'Kane'));
foreach($outer_arr as $key => $val) {
    print($key); // "kanye"
    print_r($val); // Array ( [0] => Kanya [1] => Janaye [2] => Kayne [3] => Kane )
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement