in_array() 期望参数 2 为数组,null 给定

in_array() expects parameter 2 to be array, null given in

我正在尝试 运行 一个在结果发生变化时输出的命令。但是,我不断收到 in_array() expects parameter 2 to be array, resource given in 错误。

这是我的代码:

$call = "~/www/reteps.tk/go/kahoot-auto " . '262671' . " " . 'bob' . " ";
$result = array();
$old_result = array(0 => "something");

$handle = popen($call, "r");
$result = file($handle); #is this how I read the results?
while (in_array("end",$result) != true) {
  sleep(1); 
  if ($result != $old_result) {
    echo($result);
    $old_result = $result;
  }   
}

完整错误日志:

PHP Warning:  file() expects parameter 1 to be a valid path, resource given in /home/retep/www/reteps.tk/school/kahoot2.php on line 70
PHP Warning:  in_array() expects parameter 2 to be array, null given in /home/retep/www/reteps.tk/school/kahoot2.php on line 71
PHP Warning:  in_array() expects parameter 2 to be array, null given in /home/retep/www/reteps.tk/school/kahoot2.php on line 71
PHP Warning:  in_array() expects parameter 2 to be array, null given in /home/retep/www/reteps.tk/school/kahoot2.php on line 71

你必须把文件的路径(可能是$call)放在file函数中,然后你放一个资源($result

你能提供$call的价值吗?该错误似乎表明它是资源而不是路径。 popen() 需要一个 $command:

http://php.net/manual/en/function.popen.php

所以 $call 应该是某种字符串,可以是 Linux 命令或可执行文件路径之类的。现在 $call 似乎是一个资源,所以路径或命令可能在资源内部,您只需要指向该值而不是资源本身。

//我的代码

$r=array(10.5,11,45,78,85,90);

if(in_array(4,$r)) {
    echo "number is found";
} else {
    echo "number is not found";
}

系统找不到您为文件指定的路径。因此,$result 没有任何价值。在数组中的下一个语句中,第二个参数是数组,但 $result 不是数组,因为路径无效。

你的数组代码没问题。只需检查文件的路径。它会解决你的问题。