Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return PHP array name

Tags:

arrays

php

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.

like image 229
James Avatar asked May 11 '09 11:05

James


1 Answers

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 )
}
like image 123
viam0Zah Avatar answered Oct 05 '22 02:10

viam0Zah