<?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" <[EMAIL PROTECTED]>
To: "james" <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
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: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> > To contact the list administrators, e-mail: [EMAIL PROTECTED]
> >
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]
>
>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to