note 30632 added to function.array-search
| From: | g at plzdontspam dot mindstorm dot it | Date: | Mon, 24 Mar 2003 14:01:53 +0000 |
| Subject: | note 30632 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-45736@lists.php.net to get a copy of this message | ||
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.
--
http://www.php.net/manual/en/function.array-search.php
http://master.php.net/manage/user-notes.php?action=edit+30632
http://master.php.net/manage/user-notes.php?action=delete+30632
http://master.php.net/manage/user-notes.php?action=reject+30632