php - 显示 in_array() 的结果时出错

标签 php mysql pdo

我尝试使用 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/

相关文章:

php - 如何使用 Sag 从 Apache CouchDB 文档获取附件?

javascript - 通过php函数增加HTML表格行

mysql - InnoDb 与 MyISAM 的崩溃弹性

mysql - 从 SQLAlchemy 中的插入记录中获取 ID

mysql - 在同一表上更新之前或之后触发更新

PHP PDO "select like"查询

php - 尝试在php中获取非对象简单html dom的属性

javascript - 为 WordPress 中的多个菜单项提供属性(无 .js)

php - pdo 相对于 mysql_* 或 mysqli_* 的优势

php - 扩展 PDO 准备方法以替换查询前缀