-3

我阅读了 php in_array手册并创建了一些代码行。

    $handle = fopen("/path/to/file", "a")  or die('Cannot open file:');

    $y= $command_name1.$steps1;
    $x=trim(shell_exec("grep -ri -o '$y' /path/to/file "));
    $Z=array($x);
    if (in_array($y,$z,false))
    {
            echo "thise 2 variables are already in this file";
            fclose($handle);
    }

    else {
    //write some thing on  that file
    }

但这给出了上述错误。我想知道的是,2个变量($command_name1$steps1)可以加在一起并通过搜索in_array吗?

4

2 回答 2

9

$x不是数组,所以你会得到那个错误。

看看in_array()定义和使用。

从函数签名中可以看出

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

你需要第二个参数(在这种情况下$haystack)是一个数组而不是一个标量值(无论它是什么类型)

于 2013-05-15T10:39:14.120 回答
2

shell_exec()返回字符串而不是数组,因此$x不是数组。

我的猜测是,OP 想要使用strpos()而不是shell_exec().

参考: http: //php.net/manual/en/function.shell-exec.php

于 2013-05-15T10:40:26.947 回答