如何在PHP中将多维递归数组转换为单维数组?

时间:2015-08-18 12:06:47

标签: php multidimensional-array

我有一个数组,它通过在父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
                (
                )

        )
)

5 个答案:

答案 0 :(得分:0)

所以,我假设您可以更改初始函数以创建类似第二个的数组...然后您应该像这样更新您的函数:

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;
    }

答案 1 :(得分:0)

这应该有效。对于您的用例,您的功能中不需要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;
}

答案 2 :(得分:0)

试试这个,

$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
                (
                )

        )

)

我希望这会对你有所帮助:)。

答案 3 :(得分:0)

(PHP 4&gt; = 4.0.1,PHP 5)

array_merge_recursive - 递归地合并两个或多个数组

`function array_merge_recursive_distinct(array&amp; $ array1,array&amp; $ 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;
}

}

返回$ merged; } ?&GT;`

答案 4 :(得分:0)

快到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);