drktvjp713333 2010-10-07 16:58 采纳率: 0%
浏览 25
已采纳

当其中一个值等于指定值时,PHP显示关联数组的值

I have a Mysql Recordset that I have put into an associative array so that I can reuse 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!

  • 写回答

1条回答 默认 最新

  • doukui9491 2010-10-07 17:02
    关注

    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'];
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?