简体   繁体   中英

How to get first keys on multidimentional array

$data = Array (
    [First item] => Array (
        [0] => Array (
            [1] => 2
            [2] => 2
            [3] => 2
            [4] => 2
        )
        [1] => Array (
            [1] => 2
            [2] => 2
            [3] => 2
            [4] => 2
        )
    )
    [Second Item] => Array (
       [0] => Array (
            [1] => 2
            [2] => 2
            [3] => 2
            [4] => 2
        )
    )
) 

How to echo first keys First Item , Second Item

   foreach($data as $value):
        echo 'This will print First Item / Second Item';

       foreach($value as $vals):

       echo $vals[1]; //so on...
       endforeach;

   endforeach;
foreach($data as $key=>$val){
  echo $key;
}

It's hard to know exactly what you're asking, but here's a way to display all the keys recursively.

<?
$data = Array (
    'First item' => Array (
        '0' => Array (
            '1' => 2,
            '2' => 2,
            '3' => 2,
            '4' => 2
        ),
        '1' => Array (
            '1' => 2,
            '2' => 2,
            '3' => 2,
            '4' => 2
        )
    ),
    'Second Item' => Array (
       '0' => Array (
            '1' => 2,
            '2' => 2,
            '3' => 2,
            '4' => 2
        )
    )
) ;

displayKeys($data);

function displayKeys($d) {
    foreach($d as $k=>$value) {
        echo "$k\n";
        if (is_array($value)) {
            displayKeys($value);
        }
    }
}
foreach($data as $d=>$t){
echo $d;
foreach ($t as $array)

    foreach($array as $value)
        echo $value;}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM