So, I have a function that's meant to take a multi-dimensional array and print it out as a nice html drill-down navigation:
<?php
function html_print_r($array = [], $level=0, $pass_id = "-000"){
$my_rand = rand();
if (is_array($array)){
foreach ($array as $key => $val){
echo '<p>';
for ($i = 0; $i < $level; $i++){
// Some visual effect, adding pipe symbols to show depth.
echo "|";
}
echo '<a onclick=$' . "('#" . $key . $my_rand . "').toggle()>" . $key . "</a> [+]";
html_print_r($val, true, $level + 1, $key . $my_rand);
echo "</p>";
}
} elseif(is_object($array)) {
html_print_r((array)$array, true, $level);
} else{
echo "<pre style='display:none; id='" . $pass_id . "' >" . $array . "</pre>";
}
}
The problem is if I pass it an array like
["Item1"=> ["Item1a"=>"foo"], "Item2" => "Bar"]
Instead of getting a nested result like
"Item1" [+]
| "Item1a" [+]
| | "foo"
"Item2"[+]
| "Bar"
I'm getting an incorrect printout more flat like
"Item1" [+]
""
|
"Item1z" [+]
""
|
"foo"
|
"Item2" [+]
""
|
"Bar"
I've looked over it a few times, and I can't quite figure out why. I feel like I'm missing something obvious.