1

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!

2
  • $game_video is an object if you want to work on it as an array look at get_object_vars function Commented Aug 9, 2012 at 11:50
  • What if you just drop the [] in the foreach in your comment? So: foreach($game_video as $a) ? Commented Aug 9, 2012 at 11:50

4 Answers 4

2
foreach($video_by_game as $game_video)
    if(count($game_video)) {
        echo '<div>';
        foreach($game_video as $game)
           echo $game->title.'<br />';
        echo '</div>';
    }
Sign up to request clarification or add additional context in comments.

3 Comments

You have syntax error missing a closing bracket in your first if ;)
haha thanks - Stack Overflow should add syntax check before posting comment;)
Can't believe it was so obvious, I feel stupid now. Thanks for the answer! Had to fix a syntax error though echo $game->title.'<br />';. I'll accept the answer when it lets me! CHeers!
1

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

Comments

0
<?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 ?>

Comments

0

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>";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.