PHP: Loop Through Multidimensional Array

11,517
<?php
$result = array(...);

foreach( $result['videos'] as $video )
{
  // $video is each individual item in the videos array
}
Share:
11,517
dcolumbus
Author by

dcolumbus

Updated on June 04, 2022

Comments

  • dcolumbus
    dcolumbus almost 2 years

    I need to be able to loop over only a section at a time to populate content for some Tabs. What would be the best way to loop over just the Videos array?

    This is what I've tried:

    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
                    )
    
            )
    
  • dcolumbus
    dcolumbus about 12 years
    It already is an array called $result ... and I've done that exact foreach and it didn't product anything.
  • worenga
    worenga about 12 years
    then give us your foreach code there is probably an error in there then
  • Wouter J
    Wouter J about 12 years
    @dcolumbus it works perfect here: phphulp.wouterj.nl/stack-multi-arr.php (for the source, put ?source behind the url)