in_array_recursive
Auteur: php.net - 27 oktober 2008 - 14:27 - Gekeurd door: marten - Hits: 2994 - Aantal punten: 3.75 (2 stemmen)
Voor een project waar ik op dit moment mee bezig ben zocht ik een recursieve in_array. Dit omdat ik alleen iets wil toevoegen aan de array, lees winkelmandje, als deze er nog niet in staat. Omdat in_array hier niet goed mee om kan gaan heb ik even in de comments op www.php.net gezocht en vond onderstaand script. Have fun with it.
Code:
function recursive_in_array($needle, $haystack) {
foreach ( $haystack as $stalk ) {
if ($needle == $stalk || (is_array ( $stalk ) && recursive_in_array ( $needle, $stalk ))) {
return true;
}
}
return false;
}
function recursive_in_array( $needle , $haystack ) {
foreach ( $haystack as $stalk ) {
if ( $needle == $stalk || ( is_array ( $stalk ) && recursive_in_array
( $needle , $stalk ) ) ) { return true ;
}
}
return false ;
}
of met SPL
function in_array_recursive($needle, $haystack) {
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($haystack));
foreach($it AS $element) {
if($element == $needle) {
return true;
}
}
return false;
}
function in_array_recursive( $needle , $haystack ) {
$it = new RecursiveIteratorIterator( new RecursiveArrayIterator( $haystack ) ) ;
foreach ( $it AS $element ) {
if ( $element == $needle ) {
return true ;
}
}
return false ;
}
Download code (.txt)
Stemmen
Niet ingelogd.