doucang8303 2013-01-03 10:43
浏览 27
已采纳

如何从循环创建树状数组?

Suppose I have a one-dimensional array $arr like this:

Array
(
    ['key1'] => 1
    ['key2'] => 1
    ['key3'] => 1
    ['key4'] => 1
)

I want to loop through $arr like this:

foreach($arr as $key => $a) {
   //$tree[????] = ????? Here is my concern
}

such that, doing print_r($tree) produces

Array
(
    ['key1'] => Array
    (
        ['key2'] => Array
        (
            ['key3'] => Array
            (
                ['key4'] => 1
            )
        )
    )
)

My concern is how to increase the dimension (not the values) of the array $tree inside the loop from a single dimension array $numbers such that $tree['key1']['key2']['key3'] is an array and $tree['key1']['key2']['key3']['key4'] is equal to 1.

Furthermore, if I have a single dimensional array $foo with 10 elements. I should produce another array $bar that expands to 10 dimensional array similar to the output above.

What should I do inside the foreach loop? Or instead of using loop, is there a way to produce an output like that of above from a one dimension array?


EDIT:

OK well you obviously need some kind of recursive function, but can you explain how you know that key2 should be a child of key1, key3 should be a child of key2 etc based on the data you show?

The next element of a 1D array is a child of the previous element. So if the 1D array is like this:

Array
(
    ['bar'] => 1 // I don't care of the values as of the moment
    ['foo'] => 1
    ['baz'] => 1
)

Element foo should be the child of bar, and element baz should be the child of foo for the tree array.

Ok so the actual values of everything except the last element are irrelevant?

Actually all values of the 1D array is irrelevant as of this moment. I am only concerned on constructing the tree array.

  • 写回答

2条回答 默认 最新

  • duanjue7745 2013-01-03 11:09
    关注

    The way to do this is to use references.

    function create_tree($arr) {
      $result = array();
      $ref = &$result;
      foreach ($arr as $key => $el) {
        $ref = array($key => $el);
        $ref =& $ref[$key];
      }
      return $result;
    }
    

    How it works:

    $result = array(); // an array to hold the result
    
    $ref = &$result; // start with a reference to the top level
    
    foreach ($arr as $key => $el) { // iterate over the input array
    
      $ref = array($key => $el); // create this level in the array
    
      $ref =& $ref[$key]; // change the reference to be the new deepest level
    
    }
    
    return $result; // return the result
    

    See it working

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题