PHP多维数组到平面文件夹视图

时间:2013-02-05 15:54:34

标签: php arrays multidimensional-array

我在PHP中有一个像这样的多维数组:

Array
(
    [folder1] => Array
        (
            [folder11] => Array
                (
                    [0] => index.html
                    [1] => tester.html
                )

            [folder12] => Array
                (
                    [folder21] => Array
                        (
                            [0] => astonmartindbs.jpg
                        )

                )

        )

)

并应转换为“文件路径”字符串,如下所示:

Array
(
    [0] => 'folder1/folder11/index.html'
    [1] => 'folder1/folder11/tester.html'
    [2] => 'folder1/folder12/folder21/astonmartindbs.jpg'
)

有任何想法吗?

我已经尝试了很多全部删除...这是我上一次尝试的起点:

public function processArray( $_array ) {
foreach( $_array AS $key => $value ) {
    if( is_int( $key ) ) {

    } else {
    if( is_array( $value ) ) {
        $this->processArray( $value );
    } else {

    }
    }
}
echo $this->string;

}

但我没有走到尽头......希望有人可以帮忙吗?

3 个答案:

答案 0 :(得分:5)

递归函数可能就是您要搜索的内容。以下功能将起作用:

/**
 * Flattens the array from the question
 *
 * @param array  $a       Array or sub array of directory tree
 * @param string $prefix  Path prefix of $a
 */
function flatten($a, $prefix = './') {
    $paths = array();
    foreach($a as $index => $item) {
        // if item is a string then it is a file name (or a leaf in tree)
        // prefix it and add it to paths
        if(is_string($item)) {
            $paths []= $prefix . $item;
        } else {
            // if item is a directory we call flatten on it again.
            // also we append the new folder name to $prefix
            foreach(flatten($item, $prefix . $index . '/') as $path) {
                $paths []= $path;
            }
        }         
    }
    return $paths;
}

var_dump(flatten($a));

请注意flatten()在foreach循环内调用自身,子数组作为参数。这称为“递归算法”。

答案 1 :(得分:2)

如果您喜欢SPL,可以使用RecursiveArrayIteratorRecursiveIteratorIterator来迭代平面结构。

我的结果如下:

$arr = array(); // your array
$arr = new RecursiveArrayIterator($arr);
$iterator = new RecursiveIteratorIterator($arr, RecursiveIteratorIterator::SELF_FIRST);


$currentDepth = 0;
$currentPath = array();
$result = array();

foreach($iterator as $key => $value) {
  // if depth is decreased
  if ($iterator->getDepth() < $currentDepth) {
    // pop out path values
    do {
      $currentDepth--;
      array_pop($currentPath);
    } while($iterator->getDepth() < $currentDepth);
  }

  if (is_array($value)) {
    // add parent to the path
    $currentPath[] = $key;
    $currentDepth++;
  } else {
    // add children to result array
    $result[] = implode('/', $currentPath).'/'.$value;
  }
}

转储数据将如下所示:

print_r($result);
/*
Array
(
    [0] => folder1/folder11/index.html
    [1] => folder1/folder11/tester.html
    [2] => folder1/folder12/folder21/astonmartindbs.jpg
)
*/

答案 2 :(得分:0)

在你的情况下,你需要实现一个递归函数,你试图做,这是一个简单的代码,它可以帮助你, 我不确定这是否有效:

$result = array();
$d = 0;
$tmp = "";
public function processArray( $_array ,$before) {
foreach( $_array AS $key => $value ) {
    if( is_int( $key ) ) {  // If the key is a number, then there is no a sub-array 
      $result[$d] = $before . '/' . $value;
       $d++;
      $before="";
    } else {
    if( is_array( $value ) ) { // if the value is an array, then we will add the key into string that we will return and search into subarray.
 $before = $before . '/' . $key;
        $this->processArray( $value,$before  );
    } else {


    }
    }
}
return $result;

}