我尝试使用 in_array()
比较两个数组中的值。 $friends[]
包含 Friends 表中我 friend 的所有 id,而 $membersarray[]
包含团队中人员的 id。
我在这里尝试使用in_array()
做的是,我只希望不属于团队的 friend ID出现,并且它有效。但现在,我希望结果 ($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;
}
}
最佳答案
您可以在变量名称后使用带有空括号的语法:
$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
关于php - 显示 in_array() 的结果时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27494087/