2013-09-23 123 views
1

我正在进行API调用以获取我的朋友以及为此获取的数组,我需要“过滤”具有在数据库中找到的ID的数组。现在结果只是“空”。将数组与数据库中的数据进行比较

这是数组(定义为变量$的朋友):

[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>"; 
     } 
    } 
} 

有什么建议?

UPDATE

我做了一些改变这一点,但仍然没有结果:

   $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>"; 
           } 
       }  
+0

首先使用print_r(top_list_friends)和print_r($ friends ['data'])进行调试并将其粘贴到帖子中如果可能的话 –

回答

0

试图改变$朋友数组是这样的:

$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>"; 
       } 
} 

WITHOUT的foreach ($朋友作为$朋友)LOOP :)

+0

感谢您的帮助,但我仍然没有得到任何结果。我根据你的回答在我的问题上做了更新,但也许我做错了什么? – Kim

相关问题