note 55214 added to control-structures.foreach
| From: | dyer85 at gmail dot com | Date: | Wed, 27 Jul 2005 22:44:59 +0000 |
| Subject: | note 55214 added to control-structures.foreach | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-92581@lists.php.net to get a copy of this message | ||
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 entities)
$html=true;
// Hehe, I've always used 3-spaced tabs
$indent = $html ? str_repeat(' ', $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";
}
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55214
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55214&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55214&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55214&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55214&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55214&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55214&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55214&report=yes
Search -- http://master.php.net/manage/user-notes.php