转换ul()codeigniter helper的数组结构

I want to ask how to convert an array like below:

$arr = array(
    array(
        'id' => 1,
        'name' => 'Home',
        'link_to' => 'home.php',
        'parent' => 0,
        'level' => 1
    ),
    array(
        'id' => 2,
        'name' => 'About',
        'link_to' => 'about.php',
        'parent' => 0,
        'level' => 1
    ),
    array(
        'id' => 3,
        'name' => 'About Me',
        'link_to' => 'about-me.php',
        'parent' => 2,
        'level' => 2
    ),
    array(
        'id' => 4,
        'name' => 'About Us',
        'link_to' => 'about-us.php',
        'parent' => 2,
        'level' => 2
    ),
    array(
        'id' => 5,
        'name' => 'Contact Us',
        'link_to' => 'contact-us.php',
        'parent' => 4,
        'level' => 3
    ),
    array(
        'id' => 6,
        'name' => 'Blog',
        'link_to' => 'blog.php',
        'parent' => 0,
        'level' => 1
    ),
);

into this one:

$result = array(
    'Home',
    'About' => array(
        'About Me',
        'About Us' => array(
            'Contact Us'
        )
    ),
    'Blog'
);

there are element 'parent' id which can tell the parent array ( 0 = root) and there are also element 'level'.

I need that kind of array so I can create list using ul() function from codeigniter helper.

i had to do something similar to create a tree from data rows.

So, you have to work with references, it is easier than others ways.

The next code arrive to something similar of what you want (i think it's better structure if you makes changes later)

<?php

    $arr = array(
        array(
            'id' => 1,
            'name' => 'Home',
            'link_to' => 'home.php',
            'parent' => 0,
            'level' => 1
        ),
        array(
            'id' => 2,
            'name' => 'About',
            'link_to' => 'about.php',
            'parent' => 0,
            'level' => 1
        ),
        array(
            'id' => 3,
            'name' => 'About Me',
            'link_to' => 'about-me.php',
            'parent' => 2,
            'level' => 2
        ),
        array(
            'id' => 4,
            'name' => 'About Us',
            'link_to' => 'about-us.php',
            'parent' => 2,
            'level' => 2
        ),
        array(
            'id' => 5,
            'name' => 'Contact Us',
            'link_to' => 'contact-us.php',
            'parent' => 4,
            'level' => 3
        ),
        array(
            'id' => 6,
            'name' => 'Blog',
            'link_to' => 'blog.php',
            'parent' => 0,
            'level' => 1
        ),
    );


    $refs = array();

    foreach($arr as &$item) {
        $item['children'] = array();
        $refs[$item['id']] = $item;
    }

    unset($item); // To delete the reference

    // We define a ROOT that is the top of each elements
    $refs[0] = array(
        'id' => 0,
        'children' => array()
    );

    foreach($arr as $item) {
        if($item['id'] > 0) {
            $refs[$item['parent']]['children'][] = &$refs[$item['id']];
        }
    }

    $result = $refs[0];

    unset($refs); // To delete references

    var_dump($result);

?>