duanmao1919 2018-06-05 13:11
浏览 37
已采纳

如何在以密钥值为基础的多维数组中连接信息?

I am trying to organize a multidimensional array (just one) so that a child array is always as "children" of its parent, taking into account its id.

If there are duplicates, it should join (so that only one is left).

I've already tried mixing functions like array_search, array_key_exists, array_values, etc. but to no avail.

The array I have:

array 
          0 => 
            array 
              'id' => 111
              'name' => 'A' 
              'father' => 222
              'children' => 
                array 
                  'id' => 333
                  'name' => 'B' 
                  'father' => 111
                  'children' => null
          1 => 
            array 
              'id' => 111
              'name' => 'A'
              'father' => 222
              'children' => 
                array 
                  'id' => 444
                  'name' => 'C' 
                  'father' => 111
                  'children' => null
         2 => 
            array 
              'id' => 222
              'name' => 'D'
              'father' => 0
              'children' => null

The array I want:

array 
          0 => 
            array 
              'id' => 222
              'name' => 'D' 
              'father' => 0
              'children' => 
                array 
                  'id' => 111
                  'name' => 'A' 
                  'father' => 222
                  'children' => 
                       array 
                         'id' => 333
                         'name' => 'B' 
                         'father' => 111
                         'children' = null
                       array 
                         'id' => 444
                         'name' => 'C' 
                         'father' => 111
                         'children' => null

A recursive method seems to me the best way to approach the question, but I can not implement it.

How would you solve the problem?

  • 写回答

1条回答 默认 最新

  • douju4594 2018-06-05 14:00
    关注

    I - sort of - understand what you're trying to do, but your array gets complicated very soon and searching it will be a nightmare. I suppose every person has a unique id, so why not use a onedimensional array, with the id's as keys. Like this:

    [
    111=>[
       name => A,
       father => 67,
       mother => 666,
       children => [333,444,555]
       ],
    222=>[
       name => B,
       father => 111,
       mother => 767,
       children =>[923]
       ]
    333=>[
       name => C,
       father = > 111,
       mother => 352,
       children = > ?
       ]
    767=>[
       name => D,
       father => ?,
       mother => ?,
       children => [222]
       ]
    ]
    

    Now in case you want to look something up, you only have to refer to the ID's.

    children of A:

    id_A=111
    lookup children: $array[111][children] => 333, 444, 555
    name_child = $array[333][name]
    name_child = $array[444][name]
    name_child = $array[555][name]
    

    mother of B:

    B_id = 222
    lookup mother: $array[222][mother] => 767
    mother_name = $array[767][name]
    

    sibblings of C:

    C_id = 333
    lookup father: $array[333][father] => 111
    lookup children: $array[111][children] => [333,444,555]
    name_sibbling = $array[333][name]
    name_sibbling = $array[444][name]
    name_sibbling = $array[555][name]
    

    grandparents on mothers side of B:

    Person_id => Mother_id => Mother_Father_id == grandfather
    Person_id => Mother_id => Mother_Mother_id == grandmother
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置