dongyong1400 2017-05-18 09:51
浏览 49
已采纳

PHP递归函数问题

I need to traverse the following type of structures:

            P
        /   |   \
    E1      E2     E3 .....
   / \     /  \    |
  V1  V2  V1  V2   V3  .....
  |   |   |   |    / \
  T1  T2  T3  T4  T5  T6 .....

In order to form an associative array with the following elements:

        V1(key) = [T1(E1), T3(E2), ...]
        V2(key) = [T2(E1), T4(E2), ...]
        V3(key) = [T5(E3), T6(E3), ...]
        .....

Now here comes the tricky part: the structure is actually simplified. I don't know beforehand how many E-level nodes I'll actually need to deal with (3 in the drawing), or how many V-level nodes each of them has (but at least 1 will be there), and furthermore, each V-level node may also have multiple T-nodes.

I tried using a recursion function to do this (in PHP). I'll simplify the code because it has weird methods regarding some objects that don't really matter to the discussion. My current attempt results in:

        V1(key) = [T1(E1)]
        V2(key) = [T2(E1)]

That I think means that the traversal is only occurring going down the first E-level "branch".

This is my code:

$result = [];

$traverser = function($node) use (&$traverser, &$result) {
  $children = $node->getChildrenArray();

  foreach($children as $key=>$child){
    if ($child->nodeType() == 'v_node') {
      $v_node_key = $child->name;

      $t_nodes = $child->getChildrenArray();

        if ( !array_key_exists($v_node_key, $results) ){
          $results[$v_node_key] = [];
        }

        foreach($t_nodes as $keyt=>$t_node) {
          $info_array = $t_node->toArray();
          array_push($results[$v_node_key], $info_array);
        }

    } else if ($child->nodeType() == 'e_node') {
        // keep digging
        return $traverser($child);
    }

  }
};

$traverser($p_node); 

I think the problem is that once I call the $traverser function within the foreach it won't come back and resume from the previous state.

Can anyone advise on how I should be tackling this to get the result I placed above?

  • 写回答

3条回答 默认 最新

  • dongxiao1591 2017-05-18 11:33
    关注

    Well, this is a bit awkward and I'm still not entirely sure if this is the right motive, but I solved this by removing the return in my code.

    I thought that the return would allow me to exit the nested function call, but rather I think it jumped out of the first function call (the $traverser($p_node); line).

    Even so, by changing the return $traverser($child); line to $traverser($child); it did what it had to do.

    Hope this helps anyone!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵