2012-04-08 21 views
2

一部のタブでは、一度に1つのセクションのみをループしてコンテンツを取り込む必要があります。配列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

答えて

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

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

それはすでにあると思います正しく見えます$ resultという配列があります。私はその正確なforeachを行いましたが、それは何もプロダクトではありませんでした。 – dcolumbus

+0

それから私たちにあなたのforeachコードを教えてください。おそらくそこにエラーがあります – worenga

+0

@dcolumbusそれはここで完璧に動作します:http://phphulp.wouterj.nl/stack-multi-arr.php(ソースは '?source' URLの後ろに) –