如何从数组中生成二叉树数组


How can I make a binary tree array from the array?

我有一组人:

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;
}

我怎样才能让它工作?或者可能有一些合适的图书馆?

你差点就搞定了。我会这样做:

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;
}