我试图使用foreach循环访问以下多维数组

时间:2015-10-08 11:54:53

标签: php arrays multidimensional-array foreach

$data = array(
    array(
        'name'    => 'Ahmed',
        'age'     => 24,
        'hobbies' => array('swimming','Drawing','Programming')
    )
);

我想要实现的最终输出是:

My Hobbies:1)swimming 2)Drawing 3)Programming

3 个答案:

答案 0 :(得分:0)

foreach ($data as $d) {
    echo "My hobbies:";
    foreach ($d['hobbies'] as $i => $h) {
        echo $i+1 . ")" . $h . " ";
    }
}

答案 1 :(得分:0)

如果您的阵列结构已修复,请尝试使用此代码

echo "My hobbies:";
    foreach ($data[0]['hobbies'] as $Key => $value) {
        echo $Key+1 . ")" . $value . " ";
    }

答案 2 :(得分:-1)

My Hobbies:
<?php
    foreach($data[$key]['hobbies'] as $hob_key => $hobby){
        print $hob_key . ') ' . $hobby
    }