如何为数组中的每个 child 添加一个 parent ID? php

How to add a parent ID to every child in a array? php

大家好,我想知道如何在 children 中添加 parent ID?

例如:MY_ARRAY

(
[id] => 4
[children] => Array
    (
        [0] => Array
            (
                [id] => 18
                [children] => Array
                    (
                        [0] => Array
                            (
                                [id] => 21
                            )
                        [1] => Array
                            (
                                [id] => 22
                            )
                    )
            )
        [1] => Array
            (
                [id] => 19
            )
        [2] => Array
            (
                [id] => 20
                [children] => Array
                    (
                        [0] => Array
                            (
                                [id] => 26
                            )
                    )
            )
    )
)

到目前为止,这些是我所做的代码,但仍然没有给我正确的结果。

$in = MY_ARRAY
function generateArray($in, $out, $parent = 0, $prev_par = 0){
    foreach($in as $key => $value) {
        $childs = false;
        if(isset($value['children'])){ // if there is children
            $childs = $value['children'];
            unset($value['children']);              
        }
        if($prev_par != $parent){
            $prev_par = $parent;
        }
        else{
            $prev_par = $prev_par;
        }
        $out[] = array($value['id'],$parent,$prev_par);
        if($childs){
            $parent = $value['id'];
            $out = $this->generateArray($childs, $out, $parent, $prev_par);
        }
    }
    return $out;
}

我需要添加一个 Parent ID,因为现在我可以在不显示正确的 Parent ID 的情况下检索它们的层次结构。

输出可能如下所示。只要我能加上Parent ID.

[id] => 4
[P_id] => 0
[children] => Array
    (
        [0] => Array
            (
                [id] => 18
                [P_id] => 4
                [children] => Array
                    (
                        [0] => Array
                            (
                                [id] => 21
                                [P_id] => 18
                            )
                        [1] => Array
                            (
                                [id] => 22
                                [P_id] => 18
                            )
                    )
            )
        [1] => Array
            (
                [id] => 19
                [P_id] => 4
            )

        [2] => Array
            (
                [id] => 20
                [P_id] => 4
                [children] => Array
                    (
                        [0] => Array
                            (
                                [id] => 26
                                [P_id] => 20
                            )
                    )
            )
    )
)

这里真的需要帮助谢谢你们...

Your problem is solved, check it. The function is recursively push p_id and another point is if the array contain numeric index then it will skip to push p_id.

函数为:

function generateArray($in, $parent = 0){
  foreach ($in as $key => $value) {
    if(is_numeric($key)){
        $in = $value;
        $out[$key] = generateArray($in, $parent);
    }else{
      $out[$key]=$value;
      if($key=="id"){
        $out['p_id']=$parent;
        $parent=$value;
      }elseif($key=="children"){
        $in = $value;
        $out[$key] = generateArray($in, $parent);
      }
    }
  }
  return $out;
}

工作示例:

$my_array =array(
  'id'=> 4,
  'children'=> Array(
    '0'=> Array(
      'id'=> 18,
      'children'=> Array(
          '0'=> Array(
            'id'=> 21,
          ),
          '1'=> Array(
            'id'=> 22,
          ),
        ),
    ),
    '1'=> Array(
      'id'=> 19,
    ),
    '2'=> Array(
      'id'=> 20,
      'children'=> Array(
        '0'=> Array(
          'id'=> 26,
        ),
      ),
    ),
  )
);

$newarray = generateArray($my_array);
print_r($newarray);