【问题标题】:PHP: Loop Through Multidimensional ArrayPHP:循环遍历多维数组
【发布时间】:2012-04-21 09:40:27
【问题描述】:

我需要能够一次只循环一个部分来填充某些选项卡的内容。仅循环 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 个元素足以表示数组结构

标签: php loops multidimensional-array foreach


【解决方案1】:
<?php
$result = array(...);

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

【讨论】:

  • 它已经是一个名为 $result 的数组......我已经完成了确切的 foreach 并且它没有产生任何东西。
  • 然后给我们你的foreach代码,那么那里可能有错误
  • @dcolumbus 在这里完美运行:phphulp.wouterj.nl/stack-multi-arr.php(对于源代码,请将?source 放在网址后面)
猜你喜欢
  • 2011-01-02
  • 2016-02-16
  • 2021-09-17
  • 1970-01-01
  • 2010-10-24
  • 1970-01-01
相关资源
最近更新 更多