note 84105 deleted from function.in-array by danbrown

From: Date: Mon, 08 Jun 2009 21:40:44 +0000
Subject: note 84105 deleted from function.in-array by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156498@lists.php.net to get a copy of this message
Note Submitter: kyle dot florence ~at~ gmail dot com ---- 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; ?>

« previous php.notes (#156498) next »