合并父子数组

时间:2019-01-29 10:41:37

标签: php arrays codeigniter loops parent-child

我有PHP数组。我正在尝试转换为父子数组: 我使用了递归函数,但没有得到输出。

Array
(


    [0] => Array
        (
            [id] => 1
            [subscription_name] => Yearly 
            [parent_id] => 0
        )



    [1] => Array
        (
            [id] => 22
            [subscription_name] => Yearly new               
            [parent_id] => 1
        )

    [2 => Array
        (
            [id] => 23
            [subscription_name] => Yearly new offer 
            [parent_id] => 22
        )

    [3] => Array
        (
            [id] => 24
            [subscription_name] => Weekly
            [parent_id] => 0
        )

    [4] => Array
        (
            [id] => 25
            [subscription_name] => Weekly new offer
            [parent_id] => 24
        )

)

我希望这个结果

Array
(
    [0] => Array
        (
            [id] => 1
            [subscription_name] => Yearly new offer 
            [childrens] => Array
                (
                    [0] => Array
                        (
                            [id] => 22
                        )
                    [1] => Array
                        (
                            [id] => 23
                        )   
                )

        )

    [1] => Array
        (
            [id] => 24
            [subscription_name] => Weekly new offer 
            [childrens] => Array
                (
                    [0] => Array
                        (
                            [id] => 25
                        )

                )

        )

)

我已经尝试但没有得到我想要的输出

我的PHP函数是

function tree(array $elements, $parent_id = 0) {
      echo "<pre>";
        $branch = array();
        $a=array();
        foreach ($elements as $element) {
           if ($element['parent_id'] == $parent_id && $element['subscription_type_id'] !=1 ) {
                $children = $this->tree($elements, $element['id']);
                if ($children) {
                    $element['children'] = $children;
                }
                else {
                    $element['children'] = array();
                }
                $branch[] = $element;
            }

        }
        return $branch;
    }

这是我从上述函数中获得的输出:

Array
(
    [0] => Array
    (
        [id] => 1
        [subscription_name] => Yearly 
        [children] => Array
        (
                [0] => Array
                    (
                        [id] => 22
                        [subscription_name] => Yearly new 
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 23
                                        [subscription_name] => Yearly new offer 
                                        [children] => Array
                                            (
                                            )

                                    )

                        )

                )

    )

)

[1] => Array
(
    [id] => 24
    [subscription_name] => Weekly
    [children] => Array
        (
            [0] => Array
                (
                    [id] => 25
                    [subscription_name] => Weekly new offer 
                    [children] => Array
                        (
                        )

                )

        )

)

请帮助我解决此问题。 谢谢。

3 个答案:

答案 0 :(得分:1)

真的,我的意思是说真的是天真的版本,但顺便说一句,您的数据将更易于利用,例如如果它不是父ID(例如NULL),则不要将parent_id设置为0还不明白为什么会有不同的subscription_name。我认为您应该更多地更改输入内容,而不是寻找一种复杂的算法来解析它们

function tree(array $elements) {
    $openList = [];
    $result = [];
    $id = 0;
    foreach ($elements as $key => $element) {
        if($key != 0) // I suppose the trial one is not used
        {
            if($element['parent_id'] == 0) // a root
            {
                $closeList = [];
                $openList[$element['id']] = $element['id'];
                $result[$id] = $element;
                unset($result[$id]['parent_id']);
                $result[$id]['children'] = [];
                $newOpenlist = [];
                while(count($openList) > 0)
                {
                    foreach ($elements as $key => $element) {
                        if($element['parent_id'] != 0 && in_array($element['parent_id'], $openList) && !in_array($element['parent_id'], $closeList))
                        {
                            $newOpenlist[$element['id']] = $element['id'];
                            $result[$id]['children'][] = $element;
                        }
                    }
                    foreach($openList as $item)
                    {
                        $closeList[] = $item;
                    }
                    $openList = $newOpenlist;
                    $newOpenlist = [];
                }
            }
            $id++;
        }
    }
    return $result;
}

答案 1 :(得分:1)

根据您的问题,我不太清楚subscription_name背后的逻辑。关于下面的childrens代码可能有效。

<?php

$array = [
  [
 'id' => 1,
 'subscription_name' => 'Yearly',
 'parent_id' => 0
],
   [
 'id' => 22,
 'subscription_name' => 'Yearly new',
 'parent_id' => 1
],
   [
 'id' => 23,
 'subscription_name' => 'Yearly new offer',
 'parent_id' => 22
],
   [
 'id' => 24,
 'subscription_name' => 'Weekly',
 'parent_id' => 0
],
   [
 'id' => 25,
 'subscription_name' => 'Weekly new offer',
 'parent_id' => 24
],

    ];
function find_childrens_parent_not_zero($array) {  

foreach($array as $key => $value) {

 if($value['parent_id'] != 0) {
    if(!is_array($array[$key]['childrens'])) {
      $array[$key]['childrens'] = [];
    }

    foreach($array as $k => $v) {
      if($v['parent_id'] == $value['id']) {
        array_push($array[$key]['childrens'], array('id' => $v['id']));
        unset($array[$k]['parent_id']);       
      }
    }
 }

}

return $array;
}

function find_childrens_parent_zero($array) {  

  foreach($array as $key => $value) {

   if($value['parent_id'] == 0) {
      if(!is_array($array[$key]['childrens'])) {
        $array[$key]['childrens'] = [];
      }

      foreach($array as $k => $v) {
        if($v['parent_id'] == $value['id']) {
          array_push($array[$key]['childrens'], array('id' => $v['id']));
          unset($array[$k]['parent_id']);       
        }
      }
   }

  }

  return $array;
  }
function merge_children($array) {

    foreach($array as $key => $value) {
      if($value['parent_id'] == 0) { 
        //pluck childrens of it's children
        foreach($value['childrens'] as $k => $v) {
          foreach($array as $ke => $val) {
            if($v['id'] == $val['id']) {              
              $array[$key]['childrens'] = array_merge($array[$key]['childrens'], $array[$ke]['childrens']);

            }
          }
        }
      }
    }
    return $array;
}
/**
 * Remove parent not zero elements
 */
function cleanup_array($array) {
  $result = [];
  foreach($array as $key => $value) {
    if(array_key_exists('parent_id', $value )) {
      unset($value['parent_id']); 
      array_push($result, $value);  

     }
    }
    return $result;
}
echo '<pre>';
$result_parent_not_zero = find_childrens_parent_not_zero($array);
$result_parent_zero     = find_childrens_parent_zero($result_parent_not_zero);
$result_merged_children = merge_children($result_parent_zero);
$result_cleaned_up      = cleanup_array($result_merged_children);
print_r($result_cleaned_up); 

将为您提供结果

<pre>Array
(
    [0] => Array
        (
            [id] => 1
            [subscription_name] => Yearly
            [childrens] => Array
                (
                    [0] => Array
                        (
                            [id] => 22
                        )

                    [1] => Array
                        (
                            [id] => 23
                        )

                )

        )

    [1] => Array
        (
            [id] => 24
            [subscription_name] => Weekly
            [childrens] => Array
                (
                    [0] => Array
                        (
                            [id] => 25
                        )

                )

        )

)

答案 2 :(得分:0)

NOT NULL
相关问题