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 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误