PHP:获取父子值

时间:2018-04-03 05:02:03

标签: php arrays recursion

我有一个像这样的数组

array (
  0 => 
  array (
    'id' => '1',
    'job_id' => 'J1',
    'parent_id' => '0',
  ),
  1 => 
  array (
    'id' => '2',
    'job_id' => 'J2',
    'parent_id' => '0',
  ),
  2 => 
  array (
    'id' => '3',
    'job_id' => 'J3',
    'parent_id' => '1',
  ),
  3 => 
  array (
    'id' => '4',
    'job_id' => 'J4',
    'parent_id' => '2',
  ),
  4 => 
  array (
    'id' => '5',
    'job_id' => 'J5',
    'parent_id' => '3',
  ),
  5 => 
  array (
    'id' => '6',
    'job_id' => 'J6',
    'parent_id' => '1',
  ),
  6 => 
  array (
    'id' => '8',
    'job_id' => 'J8',
    'parent_id' => '7',
  ),
)

我正在显示子记录,其中提到了parent_id的父记录

输出

Array
(
[1] => Array
    (
        [job_id] => J1
        [parent_id] => 0
        [children] => Array
            (
                [0] => Array
                    (
                        [job_id] => J3
                        [parent_id] => 1
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [job_id] => J5
                                        [parent_id] => 3
                                    )
                            )
                    )

                [1] => Array
                    (
                        [job_id] => J6
                        [parent_id] => 1
                    )

            )

    )

[2] => Array
    (
        [job_id] => J2
        [parent_id] => 0
        [children] => Array
            (
                [0] => Array
                    (
                        [job_id] => J4
                        [parent_id] => 2
                    )
            )
    )

)

现在我想 - 如果提到了parent_id,但它在给定数组中不可用,那么想要在输出中的主数组中显示记录而不是作为子数组

期待输出

Array
(
[1] => Array
    (
        [job_id] => J1
        [parent_id] => 0
        [children] => Array
            (
                [0] => Array
                    (
                        [job_id] => J3
                        [parent_id] => 1
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [job_id] => J5
                                        [parent_id] => 3
                                    )

                            )

                    )

                [1] => Array
                    (
                        [job_id] => J6
                        [parent_id] => 1
                    )

            )

    )

[2] => Array
    (
        [job_id] => J2
        [parent_id] => 0
        [children] => Array
            (
                [0] => Array
                    (
                        [job_id] => J4
                        [parent_id] => 2
                    )

            )

    )
[3] => Array
    (
        [id] => 8
        [job_id] => J8
        [parent_id] => 7
    )
 )

这里id 7不存在错误我希望它作为id为key的父记录。如何找到?

代码

function buildTree( $ar, $pid = 0 ) {
        $op = array();
        foreach( $ar as $item ) {
            echo '<pre>';
            //print_r($item);echo '<br>';//exit;
            if( $item['parent_id'] == $pid ) {
                $op[$item['id']] = array(
                    'job_id' => $item['job_id'],                       
                    'parent_id' => $item['parent_id']
                );
                // using recursion
                $children =  buildTree( $ar, $item['id'] );
                //print_r($children);
                $children = array_values($children);
                if( $children ) {
                    $op[$item['id']]['children'] = $children;
                }
            }
        }
        return $op;
 }

1 个答案:

答案 0 :(得分:0)

您可以在上面的新数组中添加pid:

  

$ op = array;   因此,每次调用buildtree都会将pid添加到新数组中。   返回后,可以在生成的$ op数组中添加其他数组。

这应该在buildtree方法之外完成: 检查是否有任何元素的parent_id不在创建的新数组中,如果是,则根据需要将该元素添加到$ op。

希望有所帮助。