tags:

views:

85

answers:

2

I am having a hard time getting the correct data out of an array. I wrote a nested foreach loop but the inner loop is throwing the "Invalid argument supplied for foreach()" error. Can someone please help me out with this? Thanks.

foreach($row as $val)
{
    echo $val['title'].'<br>';
    echo $val['author'].'<br>';
    echo $val['post'].'<br>';
    echo $val['entry_date'].'<br>';
    echo $val['comments'].'<br>';

    foreach($val as $val2)
    {
        echo $val['comments'].'<br>';
    }
}


Array
(
    [title] => First Blog
    [author] => Administrator
    [post] => Testing entry number one
    [entry_date] => Fri, 23 Oct 2009
    [comments] => Array
        (
            [0] => Array
                (
                    [commenter] => Sally Anderson
                    [comments] => comment 1 post 1
                    [comment_date] => October 24th, 2009 at 5:24 AM
                )

            [1] => Array
                (
                    [commenter] => Mike Jones
                    [comments] => comment 2 post 1
                    [comment_date] => October 24th, 2009 at 5:21 AM
                )
        )
)
Array
(
    [title] => Second Blog
    [author] => Administrator
    [post] => Testing entry number two
    [entry_date] => Sat, 24 Oct 2009
    [comments] => Array
        (
            [0] => Array
                (
                    [commenter] => Sally Anderson
                    [comments] => comment 1 post 2
                    [comment_date] => October 24th, 2009 at 5:21 AM
                )

            [1] => Array
                (
                    [commenter] => Mike Jones
                    [comments] => comment 2 post 2
                    [comment_date] => October 24th, 2009 at 5:21 AM
                )
        )
)
+3  A: 

Assuming $rows is the structure at the bottom of your question (an array of arrays) then:

foreach ($rows as $row) {
  echo <<<END
$row[title]<br>
$row[author]<br>
$row[post]<br>
$row[entry_date]<br>
END;
  foreach ($row['comments'] as $comment) {
    echo <<<END
$comment[comments]<br>
$comment[commentor]<br>
$comment[comment_date]<br>
END;
  }
}

I strongly suggest you choose meaningful names (like $comment and $row) rather than meaningless names like $val and $val2 as these are just going to cause confusion.

Also I've used heredoc syntax in the revised version as I tend to think that can make things much more readable but that's optional. A better alternative might be:

<?php foreach ($rows as $row): ?>
<?php echo $row['title'] ?><br>
<?php echo $row['post'] ?><br>
<?php echo $row['entry_date'] ?><br>
<?php foreach ($row['comments'] as $comment): ?>
<?php echo $comment['comments'] ?><br>
<?php echo $comment['commentor'] ?><br>
<?php echo $comment['comment_date'] ?><br>
<?php endforeach; ?>
<?php endforeach; ?>

which can be useful if you've got a lot of HTML interspersed. The above switched to using alternative control structures, which is optional but is often considered more readable in this type of code.

Lastly, you could PHP short tags, which some people don't like (either because they might be disabled or they interface with XML processing instructions) but I generally prefer:

<? foreach ($rows as $row): ?>
<?= $row['title'] ?><br>
<?= $row['post'] ?><br>
<?= $row['entry_date'] ?><br>
<? foreach ($row['comments'] as $comment): ?>
<?= $comment['comments'] ?><br>
<?= $comment['commentor'] ?><br>
<?= $comment['comment_date'] ?><br>
<? endforeach; ?>
<? endforeach; ?>
cletus
Hi Cletus, Thanks for the help. I tried this but I am still getting the error. I'm getting the value echo but the errors are still appearing in my log. Any ideas?
Jack
Cletus, thank you. Your solution works but I have no idea why... Thanks. I will look into the heredoc syntax as well.
Jack
@jack - spell out the foreach.... foreach (item in) $collection (as) $singular (do) etc. When you got to the nested array, you tried to treat it as a singular, whereas it's another collection! That collection ($row[comments] in your case) needs to be passed to the nested loop; foreach (item in) $comments_collection (as) $comment (do)....
deau
Hi dsclose, thanks for the explanation. It really puts it into prospective for me. Also, I'm embarrassed to admit this, but I didn't realize that I could have referred to a "collection" in a foreach like $row['comments']. This helps me greatly. Thank you al again for the great help.
Jack
A: 

change

foreach($val as $val2)
{
    echo $val['comments'].'<br>';
}

to

foreach($val['comments'] as $val2)
{
    echo $val2['comments'].'<br>';
}
Brandon H
Thanks.. I tried this but I am still getting: Invalid argument supplied for foreach() as well as undefined index. Would you know why
Jack