note 30632 deleted from function.array-search by didou
| From: | didou@php.net | Date: | Tue, 16 Dec 2003 09:31:21 +0000 |
| Subject: | note 30632 deleted from function.array-search by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-62028@lists.php.net to get a copy of this message | ||
Note Submitter: g@plzdontspam.mindstorm.it
----
While searching for an array within another array, i.e. something like:
$i = array_search(array('c','d'), array(array('a','b'),
'500', array('c','d'), '2'));
I got the following (with PHP Version 4.1.2):
Warning: Wrong datatype for first argument in call to array_search in ...
So I wrote my own array search, like:
function another_array_search($needle, $haystack, $strict = 0) {
foreach ($haystack as $key => $value) {
if ($strict) {
if ($needle === $value) $match = $key;
} else {
if ($needle == $value) $match = $key;
}
}
return $match;
}
(I tried to make it behave like the original function in PHP Version 4.1.2)
and now this works fine:
$i = another_array_search(array('c','d'),
array(array('a','b'), '500', array('c','d'),
'2'));
echo $i;
and it displays "2".
I hope it helps anyone.