php - 在数组内搜索 MYSQL 输出上的值

标签 php mysql arrays string

下面的代码似乎不起作用或在数组上找不到任何内容。我正在使用“in_array”来搜索堆栈中的针。我还尝试用逗号分隔来分解内容,但不起作用。有什么建议么?我还尝试了“array_search”。

$q4 = "SELECT domain_name,slots_config.bid FROM slots_pid,slots_config,slots_sites 
WHERE slots_config.bid=slots_pid.bid && slots_sites.aid=slots_pid.aid";
$result4 = mysql_query($q4);

while($row = mysql_fetch_array($result4))
{
    $resultarray[] = $row;
}


if (in_array("test",$resultarray))
  {
  echo "Match found";
  }
else
  {
  echo "Match not found";
  }

最佳答案

看起来你这里有一个“数组的数组”。也就是说,在 while() 循环中,$row 是一个数组,对应于 mysql 查询中的数据。所以 $resultarray 的每个元素实际上包含一个数组,而不是一个字符串。

尝试这样做:print_r($resultarray)。这将显示 $resultarray 的整个结构,您可以看到如何创建数组的数组。

要使用 in_array,您需要执行类似于 in_array("test", $resultarray[0])

的操作

关于php - 在数组内搜索 MYSQL 输出上的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/823108/

相关文章:

php - 刷新 oauth2 token google api 和 HWIOAuthBundle

php - 通过Ajax获取音频文件

在 C 中复制数组

arrays - 循环中的数组可能会丢失精度误差

php - 根据ID获取mysql数组的乘积

python - 如何为 numpy 矩阵生成干净的 x 和 y 轴?

php - Mysql条件查询

php 不向表中插入任何内容

mysql - SQL {setCharset} AUTO_INCRMENT=1' 在第 1 行

SQL 查询根据一个值排除项目