Reputation: 1522
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:
This is the output:
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
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
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
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
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