Recursive in_array (Recursief in_array)
Auteur: ArieMedia - 05 april 2011 - 12:01 - Gekeurd door: Stijn - Hits: 2994 - Aantal punten: (0 stemmen)
Had hem zelf toevallig nodig, en na 3 google-topic's besloot ik zelf deze simpele functie te schrijven.
- Update
Nu ook met strict (zoals Sam vroeg)
|
Code: |
function recursive_in_array($needle, $array = array(), $strict=false) {
if(in_array($needle, $array, $strict)) {
return true;
} else {
foreach($array as $key => $value) {
if(is_array($array[$key])) return recursive_in_array($needle, $value, $strict);
}
}
return false;
}
function recursive_in_array ($needle, $array = array(), $strict=false) { if(in_array($needle, $array, $strict)) { return true; } else { foreach($array as $key => $value) { if(is_array($array[$key])) return recursive_in_array ($needle, $value, $strict); } } return false; }
testcode:
$needle = 'nul';
$array = array('test' =>
array(1, 2, 4,
array(90, 'nul')
),
'tien', 'twaalf', 80);
if(recursive_in_array($needle, $array)) {
echo $needle .' is gevonden';
} else {
echo $needle .' zit niet in de array';
}
$needle = 'nul'; ), 'tien', 'twaalf', 80); if(recursive_in_array($needle, $array)) { echo $needle .' is gevonden'; } else { echo $needle .' zit niet in de array'; }
Download code (.txt)
|
|
Stemmen |
Niet ingelogd. |
|