Voting

: min(three, one)?
(Example: nine)

The Note You're Voting On

ravenswd at gmail dot com
15 years ago
(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.

<< Back to user notes page

To Top