【问题标题】:How can I make a binary tree array from the array?如何从数组中创建二叉树数组?
【发布时间】:2016-05-13 15:02:37
【问题描述】:

我有一组人:

array(
    array(
       'name' => 'John',
       'id' => 1,
       'mother_id' => 2,
       'father_id' => 3
    ),
    array(
       'name' => 'Lucy',
       'id' => 2,
       'mother_id' => 5,
       'father_id' => 4
    ),
    array(
       'name' => 'Jim',
       'id' => 3,
       'mother_id' => 7,
       'father_id' => 9
    ),
    array(
       'name' => 'Paul',
       'id' => 4,
       'mother_id' => 534,
       'father_id' => 54
    ),
    array(
       'name' => 'Laura',
       'id' => 5,
       'mother_id' => 554,
       'father_id' => 51
    ),
    array(
       'name' => 'Vanessa',
       'id' => 7,
       'mother_id' => 5354,
       'father_id' => 514
    ),
    array(
       'name' => 'Adam',
       'id' => 9,
       'mother_id' => 245354,
       'father_id' => 514234
    ),
);

我想得到这个:

array(
array(
    'person' => array(
        'name' => 'John',
        'id' => 1,
        'mother_id' => 2,
        'father_id' => 3
    ),
    'parents' => array(
        'mother' => array(
              'person' => array(
                  'name' => 'Lucy',
                  'id' => 2,
                  'mother_id' => 5,
                  'father_id' => 4
              ),
              'parents' => array(
                  'mother' => array(
                      'person' => array(
                          'name' => 'Laura',
                          'id' => 5,
                          'mother_id' => 554,
                          'father_id' => 51
                      ),
                      'parents' => array(...)
                  ),
                  'father' => array(
                        'person' => array(
                            'name' => 'Paul',
                            'id' => 4,
                            'mother_id' => 534,
                            'father_id' => 54
                        ),
                        'parents' => array(...)
                   ), 
              )
        ),
        'father' => ...
)

所以约翰是一个有爸爸妈妈的孩子,爸爸有妈妈和爸爸,妈妈有妈妈和爸爸等等,写一个做某事但不是我想要的脚本

function parseTree(& $tree, $root = null) {
    $return = null;

    foreach ($tree as $key=> $item){

        if ($item['id'] == $root){

            $return = [
                'person' => $item,
                'parents' => [
                    'father' => parseTree($tree, $item['father_id'])
                ]
            ];

            unset ($tree[$key]);
        }elseif($item['id'] == $root){
            $return = [
                'person' => $item,
                'parents' => [
                    'father' => parseTree($tree, $item['mother_id'])
                ]
            ];

            unset ($tree[$key]);
        }
        elseif ($root == null) {

            $return = [
              'person' => $item,
              'parents' => [
                  'father' => parseTree($tree, $item['father_id']),
                  'mother' => parseTree($tree, $item['mother_id'])
               ]
            ];

            unset ($tree[$key]);
        }
    }
    return $return;
}

我怎样才能让它工作?或者也许有一些合适的库?

【问题讨论】:

  • 这些是mysql数据库结果吗?
  • 不,它来自 neo4j
  • 可能有一种方法可以创建一个通过 Neo4j 为您执行此操作的查询。首先找出这一点,因为在数据库级别这样做是有意义的。如果你确定你做不到,那么你可以看看 PHP。
  • 已经做了,试了很多东西

标签: php arrays graph neo4j binary-tree


【解决方案1】:

你几乎拥有它。我会这样做:

function parseTree(&$tree, $root = null)
{
    $return = null;
    foreach ($tree as $key => $item) {
        if ($root == null || $item['id'] == $root) {
            $return = [
                'person' => $item,
                'parents' => [
                    'father' => parseTree($tree, $item['father_id']),
                    'mother' => parseTree($tree, $item['mother_id'])
                ]
            ];
            unset ($tree[$key]);
        }
    }
    return $return;
}

【讨论】:

  • 伙计,你是我的英雄!非常感谢!这是完美的:)
猜你喜欢
  • 2016-02-24
  • 1970-01-01
  • 2019-12-04
  • 2017-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-24
相关资源
最近更新 更多