Useful function. Better than print_r
| From: | Andrey Hristov | Date: | Fri, 24 Aug 2001 08:31:50 +0000 |
| Subject: | Useful function. Better than print_r | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-64221@lists.php.net to get a copy of this message | ||
<?php
function print_rrr($name, $element, $level){
// Returns $element as if you have to declare it using PHP syntaxis.
if (is_array($element)) {
$res = str_repeat("\t",$level).($level? "\t\"".$name.'" =>
':'$'.$name.' =
').'array(';
$counter = 1;
reset($element);
while(list($key, $val) = each($element)){
$res .= "\n".print_rrr($key,$val,$level+1);
$res .= ($counter==count($element)? '':',');
$counter++;
}
$res .= "\n".str_repeat("\t",$level).($level? "\t)":');');
} else {
$res = str_repeat("\t",$level+1).(is_numeric($name)?
$name:"'".addslashes(stripslashes($name))."'").
' => '.(is_numeric($element)?
$element:"'".addslashes(stripslashes($element))."'");
}
return $res;
}
// Usage
$a1 = array(1=>"dd","ff"=>array(1,2,3));
echo print_rrr("a1",$a1,0);
?>
The output is:
$a1 = array(
1 => 'dd',
"ff" => array(
0 => 1,
1 => 2,
2 => 3
)
);
Hope this help someone.
It is useful when in a script you modify an array and have to save changes.
Just preg_replace in the file content with the new definition.
Andrey Hristov
IcyGEN Corporation
http://www.icygen.com
99%
----- Original Message -----
From: "Philip Olson" <philip@cornado.com>
To: "james" <james@scgdev.com>
Cc: <php-general@lists.php.net>
Sent: Friday, August 24, 2001 12:23 AM
Subject: Re: [PHP] print_r question
> About the simplest way to accomplish what you want is :
>
> <pre>
> <?php print_r($var) ?>
> </pre>
>
> \newlines don't show up in browser but will in the source, check your html
> source and see how pretty it is (full of newlines).
>
> Here's an example :
>
> $string = "a\nb\nc\n";
>
> print $string; (newlines in source)
> print nl2br($string); (newlines and <br>'s in source)
>
> Regarding print_r(), use <pre> as suggested above or you _could_ do other
> things but we'll worry about later :)
>
> Regards,
> Philip
>
> On Thu, 23 Aug 2001, james wrote:
>
> > When I print an array out with print_r (php 4.06 on IIS 4.0) it does not
> > print newlines even if newlines are embedded in the array element. I
must
> > embed <br>, instead.
> >
> > The manual examples use \n and the output shows that newlines are
created.
> >
> > Am I missing something?
> >
> > Newbie, here. Hope this is the right thread for such question.
> >
> > Thanx,
> > James
> >
> >
> >
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> > For additional commands, e-mail: php-general-help@lists.php.net
> > To contact the list administrators, e-mail: php-list-admin@lists.php.net
> >
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
>