如何为数组中的每个子项添加父ID? PHP

时间:2016-10-09 05:53:03

标签: php arrays codeigniter sorting hierarchy

大家好,我想知道如何在孩子中添加父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;
}

我需要添加一个父ID,因为我现在可以在不显示correnct父ID的情况下检索其heirarchy。

输出可能如下所示。只要我可以添加父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
                            )
                    )
            )
    )
)

真的需要帮助,谢谢你们......

1 个答案:

答案 0 :(得分:4)

  

您的问题已解决,请检查。该函数是递归推送   p_id,另一点是数组是否包含数字索引   它会跳过推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);