note 85686 deleted from function.array-search by levim
| From: | levim@php.net | Date: | Mon, 24 Feb 2014 22:42:35 +0000 |
| Subject: | note 85686 deleted from function.array-search by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198654@lists.php.net to get a copy of this message | ||
Note Submitter: s0i0m at dreamevilconcepts dot com
----
I wrote a better array_search_recursive function. The other functions listed work and will evaluate
to true, however, they loop through the stack even after the needle is found: so this function will
take the overhead off the cpu =P
<?php
function array_search_recursive($needle, $haystack)
{
foreach ($haystack as $k => $v)
{
for ($i=0; $i<count($v); $i++)
if ($v[$i] === $needle)
{
return true;
break;
}
}
}
$access['admin'] = array('nick1');
$access['voice'] = array('nick2', 'nick3');
if (array_search_recursive('nick3', $access))
echo 'yes';
print_r($access);
?>
I wrote this for an access list that I have implemented into an IRC bot. See more interesting stuff
at dreamevilconcept's forum.