2012-04-08 207 views
2

我需要一次只能循環一個部分來填充某些選項卡的內容。什麼是最好的方式來循環只是Videos陣列?PHP:通過多維數組循環

這是我已經試過:

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 
       ) 

     ) 
+0

它看起來是正確的我會搜索其他錯誤+ PLS縮短陣列1-2元素足以代表陣列結構 – worenga 2012-04-08 20:11:12

回答

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

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

它已經是一個名爲$ result的數組...我已經完成了這個確切的foreach,並且它沒有產生任何東西。 – dcolumbus 2012-04-08 20:05:33

+0

然後給我們你的foreach代碼有可能是在那裏的錯誤然後 – worenga 2012-04-08 20:07:23

+0

@dcolumbus它在這裏完美的工作:http://phphulp.wouterj.nl/stack-multi-arr.php(對於源代碼,把'?源'在url後面) – 2012-04-08 20:15:10