【问题标题】:How to convert multidimensional recursive array into single dimensional array in PHP?如何在 PHP 中将多维递归数组转换为一维数组?
【发布时间】:2015-11-11 08:45:26
【问题描述】:

我有一个数组,它是通过基于父 ID 调用递归函数得出的。该数组是一个 n 级多维数组。 我想要的是把这个数组分解成一维的,这样每个孩子就在他们的父母之后。 我正在使用以下函数首先转换为递归树。

function formatTree($tree, $parent){
        $tree2 = array();
        foreach($tree as $i => $item){
            if($item['cat_parent_id'] == $parent){
                $tree2[$item['cat_id']] = $item;
                $tree2[$item['cat_id']]['submenu'] = formatTree($tree, $item['cat_id']);
            } 
        }

        return $tree2;
    }

这是我拥有的数组。

Array
(
    [58] => Array
        (
            [cat_id] => 58
            [cat_name] => Desserts
            [cat_parent_id] => 0
            [submenu] => Array
                (
                    [535] => Array
                        (
                            [cat_id] => 535
                            [cat_name] => dessert child
                            [cat_parent_id] => 58
                            [submenu] => Array
                                (
                                )

                        )

                )

        )

    [56] => Array
        (
            [cat_id] => 56
            [cat_name] => Biryani & Rice
            [cat_parent_id] => 0
            [submenu] => Array
                (
                )

        )
)

这就是我想要的。

Array
(
    [0] => Array
        (
            [cat_id] => 58
            [cat_name] => Desserts
            [cat_parent_id] => 0
            [submenu] => Array
                (

                )

        )
    [1] => Array
                    (
                        [cat_id] => 535
                        [cat_name] => dessert child
                        [cat_parent_id] => 58
                        [submenu] => Array
                            (
                            )

                    )    

    [2] => Array
        (
            [cat_id] => 56
            [cat_name] => Biryani & Rice
            [cat_parent_id] => 0
            [submenu] => Array
                (
                )

        )
)

【问题讨论】:

  • 一般来说,没有万能的答案。在您的特定情况下,您可以使用递归函数,如果它不为空,则在给定节点的子菜单上调用自身。

标签: php multidimensional-array


【解决方案1】:

所以,我假设你可以改变你的初始函数来制作一个像第二个一样的数组......然后你应该像这样更新你的函数:

function formatTree($tree, $parent){
        $tree2 = array();
        foreach($tree as $i => $item){
            if($item['cat_parent_id'] == $parent){
                $item['submenu'] = array();
                $tree2[] = $item;
                formatTree($tree, $item['cat_id']);
            } 
        }
        return $tree2;
    }

【讨论】:

  • 这会将子数组分解为单个数组,但我需要在它们的父数组之后。
【解决方案2】:

这应该可行。对于您的用例,您的函数中不需要 parent_id

function formatTree($tree){
  $tree2 = array();
  foreach($tree as $i => $item){
    $submenu = $item['submenu'];
    unset($item['submenu']);  //clear submenu of parent item
    $tree2[] = $item;
    if(!empty($submenu)){
      $sub = formatTree($submenu); //submenu's return as array in array
      $tree2[] = $sub[0]; // remove outer array
    }
  }
  return $tree2;
}

【讨论】:

    【解决方案3】:

    试试这个,

    $array = Array
    (
        "58" => Array
        (
            "cat_id" => 58,
            "cat_name" => "Desserts",
            "cat_parent_id" => 0,
            "submenu" => Array
            (
                "535" => Array
                (
                    "cat_id" => 535,
                    "cat_name" => "dessert child",
                    "cat_parent_id" => 58,
                    "submenu" => Array
                    ()
    
                )
    
            )
    
        ),
    
        "56" => Array
        (
            "cat_id" => 56,
            "cat_name" => "Biryani & Rice",
            "cat_parent_id" => 0,
            "submenu" => Array
            ()
    
        )
    );
    
    function singledimensional($array)
    {
        $res = array();
        foreach ($array as $i => $item) {
            $temparr = $item;
            $item['submenu'] = array();
            $res[] = $item;
            if (!empty($temparr['submenu']) ){
                  $child = singledimensional($temparr['submenu']);
                  $res[] =  $child[0];
            }
        }
        return $res;
    }
    
        echo '<pre>';
        print_r(singledimensional($array));
        echo '</pre>';
    

    输出:

    Array
    (
        [0] => Array
            (
                [cat_id] => 58
                [cat_name] => Desserts
                [cat_parent_id] => 0
                [submenu] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [cat_id] => 535
                [cat_name] => dessert child
                [cat_parent_id] => 58
                [submenu] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [cat_id] => 56
                [cat_name] => Biryani & Rice
                [cat_parent_id] => 0
                [submenu] => Array
                    (
                    )
    
            )
    
    )
    

    希望对你有帮助:)

    【讨论】:

      【解决方案4】:

      (PHP 4 >= 4.0.1,PHP 5)

      array_merge_recursive — 递归合并两个或多个数组

      `function array_merge_recursive_distinct ( 数组 &$array1, 数组 &$array2 ) { $merged = $array1;

      foreach ( $array2 as $key => &$value )
      {
        if ( is_array ( $value ) && isset ( $merged [$key] ) && is_array (     $merged [$key] ) )
        {
          $merged [$key] = array_merge_recursive_distinct ( $merged [$key],   $value );
      }
      else
      {
        $merged [$key] = $value;
      }
      

      }

      返回$合并; } ?>`

      【讨论】:

        【解决方案5】:

        快 2020 年了。我知道这有点晚了,但对于那些在谷歌上搜索的人来说:他们的答案很接近,但他们只是返回了父元素。我对其进行了一些调整以使其正常工作(使用array_merge)。

        function array_single_dimensional($items)
        {
            $singleDimensional = [];
            foreach ($items as $item) {
                $children =  isset($item['children']) ? $item['children'] : null; //temporarily store children if set
                unset($item['children']); //delete children before adding to new array
                $singleDimensional[] = $item; // add parent to new array
                if ( !empty($children) ){ // if has children
                    //convert children to single dimensional
                    $childrenSingleDimensional = array_single_dimensional($children);
        
                    //merge the two, this line did the trick!
                    $singleDimensional = array_merge($singleDimensional, $childrenSingleDimensional); 
                }
            }
            return $singleDimensional;
        }
        
        $singleDimensionalArray = array_single_dimensional($multidimensionalArray);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-19
          • 2019-04-28
          • 1970-01-01
          • 2018-09-08
          • 2011-08-24
          • 2019-04-03
          • 2020-08-10
          • 1970-01-01
          相关资源
          最近更新 更多