dongshi2588 2019-08-09 21:03
浏览 61

使用php中的递归函数调用,回声不会嵌套

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 "&#124;";
      }
      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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记