Using foreach() for recursion

From: Date: Tue, 11 Jan 2000 12:48:00 +0000
Subject: Using foreach() for recursion
Groups: php.version4 
Request: Send a blank email to php-version4+get-9033@lists.php.net to get a copy of this message
Should the following to functions print the same thing? <?php /* Testing recursion... */ function r_print($a, $deep) { if (is_array($a)) { while(list($k, $v) = each($a)) { r_print($a[$k], $deep+1); } } else print $a . " is $deep level(s) deep.\n"; } function f_r_print($a, $deep) { if (is_array($a)) { foreach ($a as $k) r_print($a[$k], $deep+1); } else print $a . " is $deep level(s) deep.\n"; } /* A useless array, for example purposes: */ $a = array("0", array("1", array(2, array(3 , array(4, array(5)))))); ?> r_print($a) returns expected results: 0 is 1 level(s) deep. 1 is 2 level(s) deep. 2 is 3 level(s) deep. 3 is 4 level(s) deep. 4 is 5 level(s) deep. 5 is 6 level(s) deep. f_r_print($a) returns: Illegal offset type in __FILE__ on __LINE__ main.c(302) : Freeing 0x081F4A50 (27 bytes), script=ZZZZ This is a pretty recent CVS... --Joey Smith

« previous php.version4 (#9033) next »