note 47116 added to function.array-search
| From: | scriptsatwebfiredotorg at osu1 dot php dot net | Date: | Wed, 03 Nov 2004 17:13:30 +0000 |
| Subject: | note 47116 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-79557@lists.php.net to get a copy of this message | ||
* Multi-Dimensional Array Search *
If you're searching for a function to search in Multi-Arrays,
this is probably usefull for you.
-------------------------------------------------------------
<?php
function multi_array_search($search_value, $the_array)
{
if (is_array($the_array))
{
foreach ($the_array as $key => $value)
{
$result = multi_array_search($search_value, $value);
if (is_array($result))
{
$return = $result;
array_unshift($return, $key);
return $return;
}
elseif ($result == true)
{
$return[] = $key;
return $return;
}
}
return false;
}
else
{
if ($search_value == $the_array)
{
return true;
}
else return false;
}
}
?>
-------------------------------------------------------------
It will return an Array with the keys from the original array
where your search-string was found or false. e.g.:
-------------------------------------------------------------
<?php
$foo[1]['a']['xx'] = 'bar 1';
$foo[1]['b']['xx'] = 'bar 2';
$foo[2]['a']['bb'] = 'bar 3';
$foo[2]['a']['yy'] = 'bar 4';
$foo['info'][1] = 'bar 5';
$result = multi_array_search('bar 3', $foo);
print_r($result);
?>
-------------------------------------------------------------
Output:
Array
(
[0] => 2
[1] => a
[2] => bb
)
-------------------------------------------------------------
I hope you like it ;)
greetz Udo
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47116
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47116&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47116&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47116&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47116&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47116&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47116&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47116&report=yes
Search -- http://master.php.net/manage/user-notes.php