请问一个简单的递归

WBOY
Release: 2016-06-13 12:47:00
Original
724 people have browsed it

请教一个简单的递归
$arr = array(
    array(
        'id' => 1,
        'parentid' => 0,
        'title' => 'a'
    ),
    array(
        'id' => 2,
        'parentid' => 1,
        'title' => 'a-1'
    ),
    array(
        'id' => 3,
        'parentid' => 1,
        'title' => 'a-1'
    ),
    array(
        'id' => 4,
        'parentid' => 2,
        'title' => 'a-1-1'
    )  
);

递归转换为数组如下:
$arr = array(
    array(
        'id' => 1,
        'parentid' => 0,
        'title' => 'a',
        'child' => array(
                       array(
                         'id' => 2,
                         'parentid' => 1,
                         'title' => 'a-1',
                         'child' => array(
                                    ......
                                    ) 
                       array(
                         'id' => 3,
                         'parentid' => 2,
                         'title' => 'a-2'
                       )
    ), 
    ......
);
请问要如何写这个递归呢?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template