user1532948
user1532948

Reputation:

know if a specific word exist in Array?

i have an array and i want to know if it countains a specific word or not for exemple 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
)

thanks

Upvotes: 1

Views: 588

Answers (4)

zambesianus
zambesianus

Reputation: 1249

you can iterate array and use substr_count to check that. It will return the number of times your search string occurs

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

Upvotes: 0

Ariful Islam
Ariful Islam

Reputation: 7675

$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;

Upvotes: 1

LSerni
LSerni

Reputation: 57388

If you just want to know whether the string exists, you could run stripos on the imploded string:

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

(or strpos if you want case sensitivity).

Upvotes: 1

user1457656
user1457656

Reputation:

if(array_walk($array, function($str){ return strstr($str, 'microsoft-iss 7.5'); })){
    echo "array_matches";
}

Upvotes: 1

Related Questions