note 47475 added to function.in-array
| From: | memandeemail at gmail dot com | Date: | Wed, 17 Nov 2004 15:55:07 +0000 |
| Subject: | note 47475 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-80642@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.in-array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47475
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47475&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47475&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47475&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47475&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47475&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47475&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47475&report=yes
Search -- http://master.php.net/manage/user-notes.php