note 47475 deleted from function.in-array by felipe
| From: | felipe@php.net | Date: | Sun, 30 Dec 2007 18:55:07 +0000 |
| Subject: | note 47475 deleted from function.in-array by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134478@lists.php.net to get a copy of this message | ||
Note Submitter: memandeemail at gmail dot com
----
Some very helpfuly variant's, of functions:
class shared {
/**lordfarquaad at notredomaine dot net 09-Sep-2004 11:44
* @return bool
* @param mixed $needle
* @param mixed $haystack
* @desc This function will be faster, as it doesn't compare all elements, it stops when it
founds the good one. It also works if $haystack is not an array :-)
*/
function in_array_multi($needle, $haystack)
{
if(!is_array($haystack)) return $needle == $haystack;
foreach($haystack as $value) if(shared::in_array_multi($needle, $value)) return true;
return false;
}
/**lordfarquaad function's variant1
* @return bool
* @param string $needle_key
* @param mixed $haystack
* @desc Search a key in the array and return if founded or not.
*/
function in_array_key_multi($needle_key, $haystack)
{
if(!is_array($haystack)) return $needle_key == $haystack;
foreach($haystack as $key => $value)
{
$value; // TODO: Extract the $key without seting $value
if(shared::in_array_key_multi($needle_key, $key)) return true;
}
return false;
}
/**lordfarquaad function's variant2
* @return bool
* @param string $needlekey
* @param mixed $needlevalue
* @param mixed[optional] $haystack_array
* @param string[optional] $haystack_key
* @param mixed[optional] $haystack_value
* @desc Search in array for the key and value equal.
*/
function in_array_multi_and_key($needlekey, $needlevalue, $haystack_array = null, $haystack_key =
null, $haystack_value = null)
{
if (!is_array($haystack_array)) return ($needlekey == $haystack_key and $needlevalue ==
$haystack_value);
foreach ($haystack_array as $key => $value) if (shared::in_array_multi_and_key($needlekey,
$needlevalue, $value, $key, $value)) return true;
return false;
}
}
HOW TO USE:
shared::in_array_multi(......)
it's very simple.