note 63703 deleted from function.isset by bjori

From: Date: Tue, 28 Mar 2006 19:37:50 +0000
Subject: note 63703 deleted from function.isset by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-106876@lists.php.net to get a copy of this message
Note Submitter: tikras [at] hakeris (dot) co /dot/ uk Reason: bad code ---- Just if anyone was wondering on how to check multidimensional arrays WITHOUT recrusion, i made a small function for that (i'd be happy if someone improved it). Note: This function is just to teach you the principal of not using recrusion sometimes as it can overload your memory if you are handling a lot of data: <?php function isset3($array,$key) { $items=array(); while ( count($array) != 0 ) { foreach($array as $k => $vv) { if ( $k === $key ) { return true; } if ( is_array($vv) ) { foreach($vv as $kk => $vvv) { if ( $kk === $key ) { return true; } else { $array[$kk]=$vvv; } } } unset($array[$k]); } } return false; }?> The problem with recrusion is that you are passing on the data all the time and memory is being used up very quickly. The function above really doesn't consume as much memory but it requires more CPU. And if anyone was wondering how to do it in recrusion: <?php function isset2($key,$array) { if ( isset($array[$key]) ) { return true; } else { foreach($array as $key2 => $value) { if ( is_array($value) ) { if ( in_array2($key,$value) ) { return true; } } } return false; } } ?>

« previous php.notes (#106876) next »