php要怎么对数组元素进行分组

我有一系列文件夹/路径: $arr = Array(    0 = Array         (              'name' = 'aaa'         ),    1 = Array         (              'name' = 'aaa\bbb'        ),    2 = Array         (              'name' = 'aaa\bbb\ccc'         ),    3 = Array   

我有一系列文件夹/路径:
php">$arr = Array
(
    0 => Array
         (
              'name' => 'aaa'
         ),

    1 => Array
         (
              'name' => 'aaa\bbb'
        ),

    2 => Array
         (
              'name' => 'aaa\bbb\ccc'
         ),
    3 => Array
         (
              'name' => 'ddd'
         )
);
我想将它转换为多维(树状)数组(保持结构:索引/键和值/名称):
 aaa 
    bbb
       ccc
 ddd
有什么建议和方法吗?
解决办法,尝试用如下代码:
$arr = array(
   array('name' => 'aaa'),
   array('name' => 'aaa\bbb'),
   array('name' => 'aaa\bbb\ccc'),
   array('name' => 'ddd'),
   array('name' => 'ddd\zzz'),
   array('name' => 'zzz'),
   array('name' => 'ddd\zzz\fff'),
);

$new = array();
$helper = array();
foreach ($arr as $i => $entry) {
    $parent =& $new;
    /**
     * One could use:
     *   explode(DIRECTORY_SEPARATOR, $entry['name'])
     * 编程学习网 topstudy.com.cn
     * instead of '\\' if you're dealing with file-structures
     */
    foreach ($path = explode('\\', $entry['name']) as $ii => $element) {
        $subPath = implode('.', array_slice($path, 0, $ii + 1));

        if (isset($helper[$subPath])) {
            $parent =& $helper[$subPath];
            continue;
        }

        $parent[$i] = array('name' => $element);
        $helper[$subPath] =& $parent[$i];
    }
}
print_r($new);
输出结果为:
Array
(
    [0] => Array
        (
            [name] => aaa
            [1] => Array
                (
                    [name] => bbb
                    [2] => Array
                        (
                            [name] => ccc
                        )

                )

        )

    [3] => Array
        (
            [name] => ddd
            [4] => Array
                (
                    [name] => zzz
                    [6] => Array
                        (
                            [name] => fff
                        )

                )

        )

    [5] => Array
        (
            [name] => zzz
        )

)
 

本文标题为:php要怎么对数组元素进行分组

基础教程推荐