>백엔드 개발 >PHP 튜토리얼 >如何将一个树状数组转换为一个普通的二维数组

如何将一个树状数组转换为一个普通的二维数组

WBOY
WBOY원래의
2016-06-23 14:03:011215검색

请教如何将一个树状数组转换为一个普通的二维数组 

元素的顺序不要紧~~只要是这个结构就行了

例如
Array
(
    [0] => Array
        (
            [kind_id] => 1103
            [kind_fid] => 1101
            [kind_name] => asdfsadf
            [children] => Array
                (
                    [0] => Array
                        (
                            [kind_id] => 1109
                            [kind_fid] => 1103
                            [kind_name] => testt
                            [children] => Array
                                (
                                )

                        )

                )

        )

    [1] => Array
        (
            [kind_id] => 1104
            [kind_fid] => 1101
            [kind_name] => sdfsdf
            [children] => Array
                (
                    [0] => Array
                        (
                            [kind_id] => 1110
                            [kind_fid] => 1104
                            [kind_name] => test222
                            [children] => Array
                                (
                                )

                        )

                )

        )

    [2] => Array
        (
            [kind_id] => 1107
            [kind_fid] => 1101
            [kind_name] => aaaaaaaa
            [children] => Array
                (
                )

        )

)


转为
Array
(
[0] => Array
        (
            [kind_id] => 1103
            [kind_fid] => 1101
            [kind_name] => asdfsadf
         )
[1] => Array
        (
            [kind_id] => 1109
            [kind_fid] =>1103
            [kind_name] => asdfsadf
         )
[2] => Array
        (
            [kind_id] => 1104
            [kind_fid] => 1101
            [kind_name] => asdfsadf
         )
[3] => Array
        (
            [kind_id] => 1110
            [kind_fid] => 1104
            [kind_name] => asdfsadf
         )
[4] => Array
        (
            [kind_id] => 1107
            [kind_fid] => 1101
            [kind_name] => asdfsadf
         )

)


回复讨论(解决方案)

$ar = array (  0 =>   array (    'kind_id' => '1103',    'kind_fid' => '1101',    'kind_name' => 'asdfsadf',    'children' =>     array (      0 =>       array (        'kind_id' => '1109',        'kind_fid' => '1103',        'kind_name' => 'testt',        'children' =>         array (        ),      ),    ),  ),  1 =>   array (    'kind_id' => '1104',    'kind_fid' => '1101',    'kind_name' => 'sdfsdf',    'children' =>     array (      0 =>       array (        'kind_id' => '1110',        'kind_fid' => '1104',        'kind_name' => 'test222',        'children' =>         array (        ),      ),    ),  ),  2 =>   array (    'kind_id' => '1107',    'kind_fid' => '1101',    'kind_name' => 'aaaaaaaa',    'children' =>     array (    ),  ),);print_r(untree($ar));function untree($ar) {  $res = array();  foreach($ar as $v) {    $t = $v['children'];    unset($v['children']);    $res[] = $v;    if($t) $res = array_merge($res, untree($t));  }  return $res;}
Array
(
    [0] => Array
        (
            [kind_id] => 1103
            [kind_fid] => 1101
            [kind_name] => asdfsadf
        )

    [1] => Array
        (
            [kind_id] => 1109
            [kind_fid] => 1103
            [kind_name] => testt
        )

    [2] => Array
        (
            [kind_id] => 1104
            [kind_fid] => 1101
            [kind_name] => sdfsdf
        )

    [3] => Array
        (
            [kind_id] => 1110
            [kind_fid] => 1104
            [kind_name] => test222
        )

    [4] => Array
        (
            [kind_id] => 1107
            [kind_fid] => 1101
            [kind_name] => aaaaaaaa
        )

)

版主牛叉!~

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.