note 84105 added to function.in-array
| From: | kyledotflorence~at~gmaildotcom at osu1 dot php dot net | Date: | Fri, 27 Jun 2008 21:20:14 +0000 |
| Subject: | note 84105 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140885@lists.php.net to get a copy of this message | ||
Improved (in readability, anyhow) multi-array search:
<?
// Function for looking for a value in a multi-dimensional array
function in_multi_array($value, $array)
{
foreach ($array as $key => $item)
{
// Item is not an array
if (!is_array($item))
{
// Is this item our value?
if ($item == $value) return true;
}
// Item is an array
else
{
// See if the array name matches our value
//if ($key == $value) return true;
// See if this array matches our value
if (in_array($value, $item)) return true;
// Search this array
else if (in_multi_array($value, $item)) return true;
}
}
// Couldn't find the value in array
return false;
}
?>
If you want the function to also match keys (such as if you are searching for an array called
'apple' inside of an array), uncomment this line:
<?
//if ($key == $value) return true;
?>
----
Server IP: 209.41.74.194
Probable Submitter: 70.179.13.145
----
Manual Page -- http://www.php.net/manual/en/function.in-array.php
Edit -- https://master.php.net/note/edit/84105
Del: integrated -- https://master.php.net/note/delete/84105/integrated
Del: useless -- https://master.php.net/note/delete/84105/useless
Del: bad code -- https://master.php.net/note/delete/84105/bad+code
Del: spam -- https://master.php.net/note/delete/84105/spam
Del: non-english -- https://master.php.net/note/delete/84105/non-english
Del: in docs -- https://master.php.net/note/delete/84105/in+docs
Del: other reasons-- https://master.php.net/note/delete/84105
Reject -- https://master.php.net/note/reject/84105
Search -- https://master.php.net/manage/user-notes.php