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



我在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;
}

但我并没有走到尽头。。。。希望有人能帮忙?

您可能正在搜索递归函数。以下功能将起作用:

/**
 * 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循环中调用自己,并使用子数组作为参数。这被称为"递归算法"。

如果您喜欢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
)
*/

在您的情况下,您需要实现一个您尝试过的递归函数,这里有一个简单的代码,它可能会帮助您,我不确定这是否有效:

$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;
}

相关内容

  • 没有找到相关文章