Kush
Kush

Reputation: 1522

Multiple arrays inside array, new div for each array inside an array

Okay so I have multiple arrays inside an array. For each array inside the array I want the inner array to to be echoed in a new div. I'm completely lost on how to do this

Also, it should not make a div if the array is empty.

This is the code I'm using to var_dump the following output:

code

This is the output: screenshot

I've read through php array documentation and searched on stackoverflow, I can't seem to find an answer so please help. Thanks!

Upvotes: 1

Views: 273

Answers (4)

Igor Evstratov
Igor Evstratov

Reputation: 719

You can use array_walk function to process each item of $game_video array:

if (!empty($game_video)) {
    echo "<div>";
    array_walk($game_video, function($item,$key){ 
                               echo "The necessary output";
                            });
    echo "</div>";
}

Upvotes: 0

Jerzy Zawadzki
Jerzy Zawadzki

Reputation: 1985

foreach($video_by_game as $game_video)
    if(count($game_video)) {
        echo '<div>';
        foreach($game_video as $game)
           echo $game->title.'<br />';
        echo '</div>';
    }

Upvotes: 2

Paul T. Rawkeen
Paul T. Rawkeen

Reputation: 4114

<?php foreach($video_by_game as $videos): // foreach game ?>
<?php    foreach($videos as $video): // foreach video of the game ?>
<?php      if (empty($video)) continue; // if no videos -/> skip ?>
 <div>
   Title: <?php echo $video->title ?> <br>
   Added by: <?php echo $video->added_by ?> <br>
 </div>
<?php    endforeach ?>
<?php endforeach ?>

Upvotes: 0

Ivan Pintar
Ivan Pintar

Reputation: 1881

Do not do a foreach $game_video[], do foreach $game_video.

foreach($videos_by_game as $game_video) {
    foreach($game_video as $gv) {
        // do your output
    }
}

The $game_video is already an array. By doing $game_video[], you are trying to iterate on its first element

Upvotes: 1

Related Questions