如何分组数组元素?

时间:2011-06-16 14:21:55

标签: php arrays tree

我有一系列文件夹/路径:

$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

有什么建议吗?

4 个答案:

答案 0 :(得分:5)

尝试:

$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'])
     * 
     * 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
        )

)

答案 1 :(得分:0)

$newarr=array();

foreach ($arr as $element) {
    $currentroot=$newarr;
    $pieces=explode('\\', $element);
    for ($x=0; $x<=count($pieces); x++) {
        $currentroot[$pieces[$x]]=array();
        $currentroot=$currentroot[$pieces[$x]];
    }
}

未经测试,但应该让你开始。您需要添加一个条件来检查它是否是最后一个,并使其成为字符串值而不是数组。

答案 2 :(得分:0)

对于新要求:

$arr = array(
   array('name' => 'aaa'),
   array('name' => 'aaa\bbb'),
   array('name' => 'aaa\bbb\ccc'),
   array('name' => 'ddd'),
);

function traverse(array $array) {
    $mark=array_shift($array);
    return array($mark => $array ? traverse($array) : array() );
}

$out = array();
foreach($arr as $path)
{
    ($add=traverse(explode('\\',$path['name'])))
        && $out[key($add)]=current($add)
        ;
}

输出:

array(2) {
  ["aaa"]=>
  array(1) {
    ["bbb"]=>
    array(1) {
      ["ccc"]=>
      array(0) {
      }
    }
  }
  ["ddd"]=>
  array(0) {
  }
}

旧问题:

旧问题有这些要求:

$arr = array(
   0 => 'aaa',
   1 => 'aaa\bbb',
   2 => 'aaa\bbb\ccc',
);

function traverse(array $array) {
    $mark=array_shift($array);
    return $array ? array($mark => traverse($array)) : $mark;
}

$out = array();
foreach($arr as $path)
{
    is_array($add=traverse(explode('\\',$path)))
        && $out[key($add)]=current($add)
        ;
}

经过测试,给出了这个输出:

array(1) {
  ["aaa"]=>
  array(1) {
    ["bbb"]=>
    string(3) "ccc"
  }
}

答案 3 :(得分:0)

嗯,必须在两个函数中完成,但是这里有:

// Directory Array to Hierarchy
function _DAtoH($path, $result = null)
{    
  if (empty($path))      return array();
  if (is_null($result))  $result = array();

  $path = explode(DIRECTORY_SEPARATOR, $path);
  $curr = array_shift($path);
  if (!isset($result[$curr]))
    $result[$curr] = array();
  $result[$curr] = _DAtoH(implode(DIRECTORY_SEPARATOR, $path), $result[$curr]);
  return $result;
}
function DAtoH($arr)
{
  $result = array();
  foreach ($arr as $a)
    $result = _DAtoH($a,$result);
  return $result;
}

传递底部函数(_DAtoH只是一个递归帮助器)你在原始问题(var_dump(DAtoH($arr));)中指定的数组,你应该收到:

array(2) {
  ["aaa"]=>
  array(2) {
    ["bbb"]=>
    array(1) {
      ["ccc"]=>
      array(0) {
      }
    }
    ["fff"]=>
    array(0) {
    }
  }
  ["ddd"]=>
  array(1) {
    ["eee"]=>
    array(0) {
    }
  }

}

(注意:我添加了一些文件夹路径只是为了测试它,因此fff,eee等。)