PHP:循环通过多维数组

时间:2012-04-08 20:02:37

标签: php loops multidimensional-array foreach

我需要能够一次只循环一个部分来填充某些标签的内容。循环遍历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 个答案:

答案 0 :(得分:5)

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

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