比较数组与数据库中的数据

时间:2013-09-23 09:03:41

标签: php arrays

我正在进行API调用以获取我的朋友和我得到的数组我需要“过滤”数组,其中包含在数据库中找到的ID。现在结果只是“空”。

这是数组(定义为变量$ friends):

 [0]=>
  array(2) {
    ["name"]=>
    string(17) "One friends name"
    ["id"]=>
    string(8) "FRIEND_ID"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(13) "Another friends name"
    ["id"]=>
    string(9) "ANOTHER_FRIEND_ID"
  }
  [2]=>
  array(2) {
    ["name"]=>
    string(22) "Another friends name"
    ["id"]=>
    string(9) "ANOTHER_FRIEND_ID"
  }

PHP代码,我想“用数据库中的ID过滤数组:

$query_top_list_friends = mysql_query("SELECT * FROM ".$DBprefix."users 
          WHERE center_id='" . $personal['center_id'] . "' ORDER BY 
          workouts DESC LIMIT 10");
$i=0;                            

$friends = $friends['data'];

foreach($friends as $friend) 
{                        
    while ($top_list_friends = mysql_fetch_array($query_top_list_friends))
    {                    
       if($friend['id']==$top_list_friends['fid']) 
       {
         $i++;
         echo "<div class='user'>";
         echo "<span class='number'>" . $i . "</span>";
         echo "<span class='name'>" . $top_list_friends['name'] . "</span>";
         echo "<span class='workouts'>" . $top_list_friends['workouts'] . "</span>";
         echo "</div>";
       } 
    }  
}   

有什么建议吗?

更新

我对此做了一些修改,但仍然没有结果:

                $friends = array($friends['data']);

                while ($top_list_friends = mysql_fetch_array($query_top_list_friends)) {                    

                                if($friend[$top_list_friends['fid']]) {

                                $i++;
                                    echo "<div class='user'>";
                                    echo "<span class='number'>" . $i . "</span>";
                                    echo "<span class='name'>" . $top_list_friends['name'] . "</span>";
                                    echo "<span class='workouts'>" . $top_list_friends['workouts'] . "</span>";
                                    echo "</div>";
                                } 
                }       

1 个答案:

答案 0 :(得分:0)

尝试更改$ friends数组,如下所示:

$friends = array ("FRIEND_ID"=>"One friends name", "ANOTHER_FRIEND_ID"=>"Another friends name");

然后:

while ($top_list_friends = mysql_fetch_array($query_top_list_friends)) {                    

                if($friend[$top_list_friends['fid']]) {

                $i++;
                    echo "<div class='user'>";
                    echo "<span class='number'>" . $i . "</span>";
                    echo "<span class='name'>" . $top_list_friends['name'] . "</span>";
                    echo "<span class='workouts'>" . $top_list_friends['workouts'] . "</span>";
                    echo "</div>";
                } 
}

没有foreach($ friends为$ friend)LOOP:)