知道Array中是否存在特定单词


know if a specific word exist in Array?

我有一个数组,我想知道它是否包含特定的单词,例如microsoft iis 7.5?

Array
(
[0] => HTTP/1.1 302 Found
[1] => Cache-Control: no-cache, no-store, must-revalidate, no-transform
[2] => Pragma: no-cache
[3] => Content-Length: 340
[4] => Content-Type: text/html; charset=utf-8
[5] => Expires: -1
[6] => Server:microsoft-iis 7.5
)

感谢

如果你只想知道字符串是否存在,你可以在内爆的字符串上运行stripos:

if (false !== stripos(implode("'n", $headers), "microsoft-iis"))
{
   // ...
}

(如果需要区分大小写,则使用strpos)。

if(array_walk($array, function($str){ return strstr($str, 'microsoft-iss 7.5'); })){
    echo "array_matches";
}
$stack = Array(
          '0' => 'HTTP/1.1 302 Found',
          '1' => 'Cache-Control: no-cache, no-store, must-revalidate, no-transform',
          '2' => 'Pragma: no-cache',
          '3' => 'Content-Length: 340',
          '4' => 'Content-Type: text/html; charset=utf-8',
          '5' => 'Expires: -1',
          '6' => 'Server:microsoft-iis 7.5'
);
$contain = substr_count ( implode( $stack ), 'microsoft-iis 7.5' )?'string found':'string not found';
echo $contain;

您可以迭代数组并使用substra_count进行检查。它将返回您的搜索字符串出现的次数

foreach ($your_array as $values){
$occurrence = substr_count($values,'microsoft-iis 7.5') ? 'exists' : 'does not exist';
    echo $occurrence;
}