note 96725 added to function.print-r
| From: | ptheofan at gmail dot com | Date: | Fri, 12 Mar 2010 22:04:19 +0000 |
| Subject: | note 96725 added to function.print-r | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-166983@lists.php.net to get a copy of this message | ||
Just saw there are plenty print_r replacement functions here. Well, I wrote one for my convenience
for my Debug class. I did minor modification to pull it out of the class. For what it's
worth...
<?php
/**
* print_r replacement with sense.
*/
define("TAB", " ");
$indent = TAB;
foreach($arr as $k => $v)
{
echo "Key ($k)</br>";
printArray($v, $k, $indent);
echo "</br>";
}
function printArray($array, $parent, $indent)
{
if($array)
{
foreach($array as $k => $v)
{
if(is_array($v))
{
echo $indent."Nested Key($k)</br>";
echo $indent."{</br>";
printArray($v, $k, $indent.TAB);
echo $indent."}</br>";
}
else
{
echo $indent."key($k) = value($v)</br>";
}
}
}
}
?>
and here's some test code. I actually wrote this as an exercise for a friend, 'cause he
wanted to test his array related skills at php, so I gave him hell :)
So, here's the code. Copy, paste and check enjoy
<?php
// $arr is the target array that will hold the results to be rendered
$arr = array();
$GLOBALS["a"] = array(0 => array("a" => 1, "b" => 2),
1 => array("c" => 3, "d" => 4),
2 => array("e" => 5, "f" => 6, "g" => 7),
3 => array("h" => 8));
// the root array holds 10 nested arrays. The nesting level is configured to a maximum of 10
// run this file multiple times to identify the random levels of nested arrays
for($i=0;$i<10;$i++)
{
$count = 0;
buildArray($arr[], $count);
}
// function to build a nested array.
// Note* RECURSIVE function
function buildArray(&$array, &$count)
{
if($count==10)
return;
else
$count++;
if(rand(0,1))
$array[] = $GLOBALS["a"][rand(0, count($GLOBALS["a"])-1)];
else
{
$array[] = $GLOBALS["a"][rand(0, count($GLOBALS["a"])-1)];
buildArray($array[], $count);
}
}
// Lay the html page...
echo '
<HTML><BODY>
<table style="border: 1px black solid">
<tr>
<td width="50%" style="vertical-align: top">
<pre>
'.print_r($arr,1).'
</pre>
</td>
<td width="50%" style="border-left: 1px black dotted; vertical-align:
top"><pre>';
// Now print with our custom function. For clarity and simplicity used straight forward echo,
// instead of creating buffers. But, in real world this better output at a buffer, and support
// the true/false second argument of print_r
define("TAB", " "); // represents the TAB (spaces per indentation level)
$indent = TAB; // init $indent to tab
// Primary foreach (running through root level)
foreach($arr as $k => $v)
{
echo "Key ($k)</br>";
printArray($v, $k, $indent);
echo "</br>";
}
// Print the nested arrays
// Note* RECURSIVE function
function printArray($array, $parent, $indent)
{
if($array)
{
foreach($array as $k => $v)
{
if(is_array($v))
{
echo $indent."Nested Key($k)</br>";
echo $indent."{</br>";
printArray($v, $k, $indent.TAB);
echo $indent."}</br>";
}
else
echo $indent."key($k) = value($v)</br>";
}
}
}
// Lay the rest of the HTML code
echo '</pre>
</td>
</tr>
</table></BODY></HTML>';
?>
------- Here's some sample output to get an idea
Key (0)
Nested Key(0)
{
key(c) = value(3)
key(d) = value(4)
}
Nested Key(1)
{
Nested Key(0)
{
key(h) = value(8)
}
}
------- while print_r would output
Array
(
[0] => Array
(
[0] => Array
(
[c] => 3
[d] => 4
)
[1] => Array
(
[0] => Array
(
[h] => 8
)
)
)
----
Server IP: 69.147.83.197
Probable Submitter: 94.66.0.109
----
Manual Page -- http://www.php.net/manual/en/function.print-r.php
Edit -- https://master.php.net/note/edit/96725
Del: integrated -- https://master.php.net/note/delete/96725/integrated
Del: useless -- https://master.php.net/note/delete/96725/useless
Del: bad code -- https://master.php.net/note/delete/96725/bad+code
Del: spam -- https://master.php.net/note/delete/96725/spam
Del: non-english -- https://master.php.net/note/delete/96725/non-english
Del: in docs -- https://master.php.net/note/delete/96725/in+docs
Del: other reasons-- https://master.php.net/note/delete/96725
Reject -- https://master.php.net/note/reject/96725
Search -- https://master.php.net/manage/user-notes.php