note 84231 deleted from function.print-r by danbrown
| From: | danbrown@php.net | Date: | Wed, 16 Jul 2008 20:36:33 +0000 |
| Subject: | note 84231 deleted from function.print-r by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-141592@lists.php.net to get a copy of this message | ||
Note Submitter: blasterjr at gmail dot com
----
i figured that I'd just submit a quick function, for those of you who have used the print_r
function, and wish you could take the data from there, exactly how its formatted, inside and out,
well here's the function for you, its made for plain text, but i figured it'd be nice,
I'm using it to store information about posts and gets, its recursive, so it supports
multidimensional arrays, along with full alignment almost exactly (if not exactly) like the print_r
function, took a bit to figure out, but it works like a charm
<?
function Array2String($array,$level=0)
{
$i = 0;
$j = 0;
$k = 0;
$l = 0;
while($i < $level)
{
$string .= ' ';
$i++;
}
$string = "Array \n";
if($level > 0)
{
$level++;
}
while($j < $level)
{
$string .= ' ';
$j++;
}
$string .= "(\n";
foreach($array as $key => $value)
{
while($k <= $level)
{
$string .= ' ';
$k++;
}
$k = 0;
if(is_array($value))
{
$newLevel = (int)$level+1;
$string .= "[".$key."] => ".Array2String($value,$level+1);
}
else
{
$string .= "[".$key."] => ".$value."\n";
}
}
while($l < $level)
{
$string .= ' ';
$l++;
}
$string .= ") \n";
if($level > 0)
{
$string .= "\n";
}
return $string;
}
?>