note 63703 added to function.isset

From: Date: Tue, 28 Mar 2006 19:30:32 +0000
Subject: note 63703 added to function.isset
Groups: php.notes 
Request: Send a blank email to php-notes+get-106870@lists.php.net to get a copy of this message
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; } } ?> ---- Server IP: 84.18.202.180 Probable Submitter: 84.9.162.99 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.isset.php Edit -- http://master.php.net/note/edit/63703 Del: integrated -- http://master.php.net/note/delete/63703/integrated Del: useless -- http://master.php.net/note/delete/63703/useless Del: bad code -- http://master.php.net/note/delete/63703/bad+code Del: spam -- http://master.php.net/note/delete/63703/spam Del: non-english -- http://master.php.net/note/delete/63703/non-english Del: in docs -- http://master.php.net/note/delete/63703/in+docs Del: other reasons-- http://master.php.net/note/delete/63703 Reject -- http://master.php.net/note/reject/63703 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#106870) next »