我有一个Mysql Recordset,我已将其放入一个关联数组中,以便我可以反复使用它。
我使用此函数将值放入数组中:
while(($Comments[] = mysql_fetch_assoc($rsComments)) || array_pop($Comments));
以下是print_r($ Comments)显示的内容
Array ( [0] => Array ( [CommentID] => 10 [Comment] => Ouch [CommentAuthor] => Randy Krohn [CommentDate] => 2010-10-06 17:19:49 [ID] => 1231 [CategoryID] => 42 ) [1] => Array ( [CommentID] => 12 [Comment] => This is the Dirty Duck [CommentAuthor] => John Lemoine [CommentDate] => 2010-10-06 17:22:43 [ID] => 1411 [CategoryID] => 42 ) [2] => Array ( [CommentID] => 13 [Comment] => Talk about deja vu! [CommentAuthor] => dber [CommentDate] => 2010-10-06 17:24:48 [ID] => 1473 [CategoryID] => 42 ) )
我循环浏览图像列表,我想只显示与指定ImageID图像相关的注释(例如1473)。
我只需要显示ID等于指定值的那些?
这一定很容易,但出于某种原因,它只是飞过我的脑袋。
谢谢你的帮助!
答案 0 :(得分:1)
最简单的方法是使用foreach
遍历'Comments'关联数组(也就是字典)并检查'ID'键的值,如果它与所需的值匹配,则打印'Comment'键的值:
$imageId = 1473;
foreach($Comments as $comment) {
if($comment['ID'] == $imageId) {
echo $comment['Comment'];
}
}