note 55214 deleted from control-structures.foreach by felipe

From: Date: Tue, 29 Jan 2008 11:24:29 +0000
Subject: note 55214 deleted from control-structures.foreach by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-135523@lists.php.net to get a copy of this message
Note Submitter: dyer85 at gmail dot com ---- Concerning mike's post below, the list and each function style would also work: <?php /* Tweak of Mike's original code */ /* Additions include Friendly HTML format or normal spaces for command line */ /* May you never be forced to deal with a structure like this in real life... */ $items = array( -1, 0, array( array(1,2,3,4), array(5,6,7,8), array(9,10,11,12) ), array( array("a", "b", "c"), array("d", "e", "f"), array("g", "h", "i") ) ); // HTML formatting echo "<pre>\n"; print_array($items, 0); echo "</pre>\n"; function print_array($arr, $level) { $indent=''; // Change to false if you want CLI formatting (no &nbsp; entities) $html=true; // Hehe, I've always used 3-spaced tabs $indent = $html ? str_repeat('&nbsp;', $level * 3) : $indent = str_repeat(' ', $level *3); // Changed from foreach while ( list(, $item) = each($arr) ) { if (is_array($item)) { echo $html ? $indent . "Item is an array...<br>\n" : $indent . "Item is an array...\n"; print_array($item, $level+1); } else { echo $html ? $indent . "Value at current index: " . $item . "<br>\n" : $indent . "Value at current index: " . $item . "\n"; } } } ?>

« previous php.notes (#135523) next »