Help with PHP arrays and foreach
Hey guys. I'm building a simple blog and have my data returning from my database in an array. I want to show the comments associated with each blog post but I'm not sure how to organize this in a loop. This is my array:
Array
(
[author] => Administrator
[post] => Testing entry number one
[entry_date] => Fri, 23 Oct 2009
[commenter] => Sally Anderson
[comments] => comment 1 post 1
[comment_date] => October 24th, 2009 at 5:24 AM
[blog_id] => 1
)
Array
(
[author] => Administrator
[post] => Testing entry number two
[entry_date] => Sat, 24 Oct 2009
[commenter] => Sally Anderson
[comments] => comment 1 post 2
[comment_date] => October 24th, 2009 at 5:21 AM
[blog_id] => 2
)
Array
(
[author] => Administrator
[post] => Testing entry number one
[entry_date] => Fri, 23 Oct 2009
[commenter] => Mike
[comments] => comment 2 post 1
[comment_date] => October 24th, 2009 at 5:21 AM
[blog_id] => 1
)
Array
(
[author] => Administrator
[post] => Testing entry number two
[entry_date] => Sat, 24 Oct 2009
[commenter] => Mike
[comments] => comment 2 post 2
[comment_date] => October 24th, 2009 at 5:21 AM
[blog_id] => 2
)
Notice that each post repeats based on the number of comments associated with it. In this parti开发者_高级运维cular case this array has 2 comments associated with each post. Could someone please help me write a foreach loop that will output 2 posts with the correct comments for each one?
Thanks!
Maybe I'm not explaining well enough. Here is what I am looking for.
Array
(
[author] => Administrator
[post] => Testing entry number one
[entry_date] => Fri, 23 Oct 2009
[commenter] => Sally Anderson
[comments] =>
**[0] => Array
(
comment 1 post 1
comment 2 post 1
)**
[comment_date] => October 24th, 2009 at 5:24 AM
[blog_id] => 1
)
I would approach the subject in a different way. During your fetch results loop, I would create an array (or an object), based on blog ID. Each element of that array would contain all the common fields of the blog entry, plus an additional array to store all the comments.
$entries = Array();
while ($row = mysql_fetch_row($result)) {
$key = $row['blog_id'];
$entries[$key]['author'] = $row['author'];
...
$entries[$key]['comments'][] = Array (
'commenter' => $row['commenter'],
'comments' => $row['comments'],
'comment_date'=> $row['comment_date']
);
}
This snippet will create the following array:
Array
(
[author] => Administrator
[post] => Testing entry number one
[entry_date] => Fri, 23 Oct 2009
[comments] = Array
(
Array
(
[commenter] => Sally Anderson
[comments] => comment 1 post 1
[comment_date] => October 24th, 2009 at 5:24 AM
)
(
[commenter] => Mike
[comments] => comment 2 post 1
[comment_date] => October 24th, 2009 at 5:21 AM
)
)
)
Array
(
[author] => Administrator
[post] => Testing entry number two
[entry_date] => Sat, 24 Oct 2009
[comments] = Array
(
Array
(
[commenter] => Sally Anderson
[comments] => comment 1 post 2
[comment_date] => October 24th, 2009 at 5:21 AM
)
(
[commenter] => Mike
[comments] => comment 2 post 2
[comment_date] => October 24th, 2009 at 5:21 AM
)
)
)
Now during your presentation loop, you iterate through your array to get the blog posts (so functions such as 'count' will also give you the number of blog entries) and when you display each entry you just iterate through the comments element.
you mean something like that which sorts your array first?
$sorted = array();
foreach ($unsorted AS key => $value) {
$sorted[$value['blog_id']][] = $value;
}
(untested)
精彩评论