1

我正在调用 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>";
                                } 
                }       
4

1 回答 1

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) 循环 :)

于 2013-09-23T09:10:46.680 回答