显示in_array()的结果时出错

时间:2014-12-15 22:10:32

标签: php mysql pdo

我尝试使用in_array()比较两个数组中的值。 $friends[]包含朋友表中朋友的所有ID,而$membersarray[]包含团队中的人员ID。

我在这里使用in_array()尝试做的是,我只想让不属于团队的朋友们出现并且有效。但现在,我希望结果($t)显示存储在friends表中的姓氏,而不仅仅是id。

例如,它显示:3,5和7.我希望它显示,'john','doe'和'rick'。我该怎么做呢?

$friends[] = $record5['id']; //contains of ids of all friends

$membersarray[] = $output['id']; //contains ids of all team members

foreach ($friends AS $t) {

   if (in_array($t, $membersarray)) {
   //donothing                         
   } else {
     echo $t;

     }
  }

1 个答案:

答案 0 :(得分:0)

您可以在变量名后使用带有空括号的语法:

$friends[] = $record5['id']; //adds one element to the array $friends

将单个元素添加到数组中。

只要$record5['id']已经是一个数组,你就需要这个:

$friends = $record5['id']; //contains ids of all friends

$membersarray = $output['id']; //contains ids of all team members
相关问题