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


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

我想要达到的最终输出是:

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

如果你的数组结构是固定的,试试下面的代码

echo "My hobbies:";
    foreach ($data[0]['hobbies'] as $Key => $value) {
        echo $Key+1 . ")" . $value . " ";
    }
My Hobbies:
<?php
    foreach($data[$key]['hobbies'] as $hob_key => $hobby){
        print $hob_key . ') ' . $hobby
    }

相关内容

  • 没有找到相关文章

最新更新