note 114230 deleted from function.array-search by levim
| From: | levim@php.net | Date: | Mon, 24 Feb 2014 22:46:12 +0000 |
| Subject: | note 114230 deleted from function.array-search by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198663@lists.php.net to get a copy of this message | ||
Note Submitter: williamprogphp at yahoo dot com dot br
----
If you need to perform a search inside an array looking for a regex pattern, you can try it
<?php
function array_preg_search($needle, $haystack) {
if (!is_scalar($needle)) { return null; }
if (!is_array($haystack)) { return null; }
foreach ($haystack as $key => $val) {
if ( preg_match($needle, $val) ) {
return $key;
}
}
return false;
}
?>
Use:
<?php
$array = Array
(
'Connected to database',
'Email sent to <> williamprogphp at yahoo dot com dot br with 2
attachment(s).',
'Disconnected to database',
'{"status":"OK","sentId":31077}',
);
echo array_preg_search('/OK/', $array);
?>
will output the key associated with.
3
Cheers