现在有一个这样的数组如下(通过递归生成的数组)
Array
(
    [id] => 98
    [name] => T-shirt
    [parent_id] => 81
    [level] => 2
    [path] => /1/81/98
    [type] => 
    [status] => active
    [image_file] => 98.jpg
    [description] => 
    [position] => 0
    [subCate] => Array
        (
            [id] => 81
            [name] => Big Dog
            [parent_id] => 1
            [level] => 1
            [path] => /1/81
            [type] => 
            [status] => active
            [image_file] => 81.jpg
            [description] => 
            [position] => 0
            [subCate] => Array
                (
                    [id] => 1
                    [name] => Printing
                    [parent_id] => 0
                    [level] => 0
                    [path] => /1
                    [type] => 
                    [status] => active
                    [image_file] => 
                    [description] => 
                    [position] => 1
                )        ))怎么将其转化为
array(
   [0] = array( 
            [id] => 98
            [name] => T-shirt
            [parent_id] => 81
            [level] => 2
            [path] => /1/81/98
            [type] => 
            [status] => active
            [image_file] => 98.jpg
            [description] => 
            [position] => 0
        )
   [1] = array( 
             [id] => 81
            [name] => Big Dog
            [parent_id] => 1
            [level] => 1
            [path] => /1/81
            [type] => 
            [status] => active
            [image_file] => 81.jpg
            [description] => 
            [position] => 0
        )
   [2] = array( 
                   [id] => 1
                    [name] => Printing
                    [parent_id] => 0
                    [level] => 0
                    [path] => /1
                    [type] => 
                    [status] => active
                    [image_file] => 
                    [description] => 
                    [position] => 1
        )
)

解决方案 »

  1.   

    function convert($arr){
        global $newArr;
        foreach($arr as $k => $v){
            $newArr[] = $v;
            if(isset($v[subCate])){
                convert($v[subCate]);
            }
        }
    }
    convert($arr);
    echo $newArr;
      

  2.   

    精简了的数组你的数组结构每个元素,至多只能有一个儿子
    $arr = array(
    'id' => 1,
    'subCate' => array(
    'id' => 81,
    'subCate' => array(
    'id' => 98
    ),
    ),
    );$res = array();
    newArray($arr);
    print_r($res);//这里打印
    function newArray($arr, $parent_id = 0)
    {
    global $res;
    $arr['parent_id'] = $parent_id;
    if(isset($arr['subCate']))
    {
    newArray($arr['subCate'], $arr['id']);
    unset($arr['subCate']);
    }
    $res[] = $arr;
    }