PHP:遍历多维数组

3

我需要能够循环遍历每个选项卡中的内容区域,以填充一些标签页的内容。最好的方法是如何仅循环遍历Videos数组?

这是我尝试过的:

foreach ($result['videos'] as $r) {
    $content = '<tr>';
    $content .= '<td>' . $r['id'] . '</td>';
    $content .= '<td>' . $r['name'] . '</td>';
    $content .= '<td>' . $r['body'] . '</td>';
    $content .= '<td>' . $r['created'] . '</td>';
    $content .= '<td>' . $r['modified'] . '</td>';
    $content .= '</tr>';
    echo $content;

}



Array
(
    [images] => Array(...)

    [videos] => Array
        (
            [0] => Array
                (
                    [id] => 7
                    [type] => 2
                    [name] => My Video
                    [body] => my_video.flv
                    [created] => 0000-00-00 00:00:00
                    [modified] => 2012-04-07 00:00:00
                )

            [1] => Array
                (
                    [id] => 13
                    [type] => 2
                    [name] => Yet another video
                    [body] => my_vid_man.flv
                    [created] => 0000-00-00 00:00:00
                    [modified] => 0000-00-00 00:00:00
                )

            [2] => Array
                (
                    [id] => 25
                    [type] => 2
                    [name] => asasd
                    [body] => asdasd
                    [created] => 0000-00-00 00:00:00
                    [modified] => 0000-00-00 00:00:00
                )

        )

它看起来没问题,我会在其他地方搜索错误 + 请缩短数组,1-2个元素足以表示数组结构。 - worenga
1个回答

6
<?php
$result = array(...);

foreach( $result['videos'] as $video )
{
  // $video is each individual item in the videos array
}

它已经是一个名为 $result 的数组了...我已经使用了相同的 foreach,但它没有产生任何结果。 - dcolumbus
请提供您的foreach代码,那里可能有错误。 - worenga
@dcolumbus 在这里完美运行:http://phphulp.wouterj.nl/stack-multi-arr.php(要查看源代码,请在网址后面加上“?source”) - Wouter J

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接