开发者

PHP show values of associative array when one of the values equals a specified value

开发者 https://www.devze.com 2023-01-18 22:56 出处:网络
I have a Mysql Recordset that I have put into an associative array so that I can r开发者_Python百科euse it over and over.

I have a Mysql Recordset that I have put into an associative array so that I can r开发者_Python百科euse it over and over.

I used this function to put the values into the array:

while(($Comments[] = mysql_fetch_assoc($rsComments)) || array_pop($Comments));

Here is what the print_r($Comments) displays

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 ) )

I am looping through a list of images, and I want to display only the comments associated with an images specified ImageID (for example 1473).

I need to display only the ones where the ID is equal to a specified value?

This must be easy, but for some reason, it is just flying over my head.

Thanks for you help!


Simplest way is to loop through 'Comments' associative array (aka dictionary) with foreach and check the value of 'ID' key, if it matches the desired value, print the value of 'Comment' key:

$imageId = 1473;
foreach($Comments as $comment) {
    if($comment['ID'] == $imageId) {
        echo $comment['Comment'];
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消