note 92065 added to function.var-export
| From: | ravenswd at gmail dot com | Date: | Tue, 07 Jul 2009 17:32:07 +0000 |
| Subject: | note 92065 added to function.var-export | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158024@lists.php.net to get a copy of this message | ||
(This replaces my note of 3-July-2009. The original version produced no output if a variable
contained an empty array, or an array consisting only of empty arrays. For example,
$bigarray['x'] = array(); Also, I have added a second version of the function.)
The output can be difficult to decipher when looking at an array with many levels and many elements
on each level. For example:
<?php
print ('$bigarray = ' . var_export($bigarray, true) . "\n");
?>
will return:
$bigarray = array(
... (500 lines skipped) ...
'mod' => 'charlie',
Whereas the routine below can be called with:
<?php
recursive_print ('$bigarray', $bigarray);
?>
and it will return:
$bigarray = array()
... (500 lines skipped) ...
$bigarray['foo']['bar']['0']['somethingelse']['mod']
= 'charlie'
Here's the function:
<?php
function recursive_print ($varname, $varval) {
if (! is_array($varval)):
print $varname . ' = ' . $varval . "<br>\n";
else:
print $varname . " = array()<br>\n";
foreach ($varval as $key => $val):
recursive_print ($varname . "['" . $key . "']", $val);
endforeach;
endif;
}
?>
For those who want a version that produces valid PHP code, use this version:
<?php
function recursive_print ($varname, $varval) {
if (! is_array($varval)):
print $varname . ' = ' . var_export($varval, true) . ";<br>\n";
else:
print $varname . " = array();<br>\n";
foreach ($varval as $key => $val):
recursive_print ($varname . "[" . var_export($key, true) . "]", $val);
endforeach;
endif;
}
?>
If your output is to a text file and not an HTML page, remove the <br>s.
----
Server IP: 208.69.120.35
Probable Submitter: 67.165.168.203
----
Manual Page -- http://www.php.net/manual/en/function.var-export.php
Edit -- https://master.php.net/note/edit/92065
Del: integrated -- https://master.php.net/note/delete/92065/integrated
Del: useless -- https://master.php.net/note/delete/92065/useless
Del: bad code -- https://master.php.net/note/delete/92065/bad+code
Del: spam -- https://master.php.net/note/delete/92065/spam
Del: non-english -- https://master.php.net/note/delete/92065/non-english
Del: in docs -- https://master.php.net/note/delete/92065/in+docs
Del: other reasons-- https://master.php.net/note/delete/92065
Reject -- https://master.php.net/note/reject/92065
Search -- https://master.php.net/manage/user-notes.php