4

假设我有这个字符串,我想把它放在一个多维数组中。

编辑:字符串中的子文件夹数量是动态的..从零子文件夹到 10

<?php
       $string ="Folder1/Folder2/Folder3/filename1\n";
       $string .=" Folder1/Folder2/Folder3/filename2\n";
       $string .=" Folder4/Folder2/Folder3/filename3\n";
?>

我想要返回以下数组

<?php
 Array
(
    [Folder1] => Array
        (
            [Folder2] => Array
                (
                    [Folder3] => Array
                        (
                            [0] => filename1
                            [1] => filename2
                        )

                )

        )

    [Folder4] => Array
        (
            [Folder2] => Array
                (
                    [Folder3] => Array
                        (
                            [0] => filename3
                        )

                )

        )

)
?>

实现这一目标的最有效方法是什么?

为了好玩,假设这个数组将被发送到世界的另一端,它想返回一个字符串。我们将如何做到这一点?

4

3 回答 3

10

你可以借用一些代码这个类(链接不再可用),具体_processContentEntry方法。

这是完成这项工作的方法的修改版本:

function stringToArray($path)
{
    $separator = '/';
    $pos = strpos($path, $separator);

    if ($pos === false) {
        return array($path);
    }

    $key = substr($path, 0, $pos);
    $path = substr($path, $pos + 1);

    $result = array(
        $key => stringToArray($path),
    );

    return $result;
}

的输出

var_dump(stringToArray('a/b/c/d'));

将会

array(1) {
  ["a"]=>
  array(1) {
    ["b"]=>
    array(1) {
      ["c"]=>
      array(1) {
        [0]=>
        string(1) "d"
      }
    }
  }
}

我想这就是你需要的:)


更新

根据您的评论,以下是处理由换行符分隔的字符串的方法:

$string = "Folder1/Folder2/Folder3/filename1\n";
$string .= " Folder1/Folder2/Folder3/filename2\n";
$string .= " Folder4/Folder2/Folder3/filename3\n";

// split string into lines
$lines = explode(PHP_EOL, $string);

// trim all entries
$lines = array_map('trim', $lines);

// remove all empty entries
$lines = array_filter($lines);

$output = array();

// process each path
foreach ($lines as $line) {
    // split each line by /
    $struct = stringToArray($line);

    // merge new path into the output array
    $output = array_merge_recursive($output, $struct);
}

print_r($output);

PS要将此数组转换为字符串,只需调用json_encode,但是我认为没有理由将其转换为数组然后再转换回原来的样子。

于 2012-04-12T12:39:02.700 回答
0

这可以通过从数组的开头获取项目并在到达最后一个项目时返回它以另一种方式递归解决。

function make_tree( $arr ){
   if( count($arr) === 1){
      return array_pop( $arr );
   }else{
      $result[ array_shift( $arr )] = make_tree( $arr ) ; 
   }
   return $result;
}

$string  = "Folder1/Folder2/Folder3/filename1\n";
$string .= "Folder1/Folder2/Folder3/filename2\n";
$string .= "Folder4/Folder2/Folder3/filename3\n";

$string = trim( $string );

$files_paths = explode( PHP_EOL, $string);

$result = [];

foreach ($files_paths as $key => $value) {
   $parted = explode( '/', $value );
   $tree = make_tree( $parted );
   $result = array_merge_recursive( $result, $tree );
   
}
var_dump( $result );
于 2020-10-19T17:08:13.320 回答
-1

我想这是你想要的,

$string ="Folder1/Folder2/Folder3/filename1\n";
$string .="Folder1/Folder2/Folder3/filename2\n";
$string .="Folder4/Folder2/Folder3/filename3\n";


$string_array_1 = explode("\n", $string);

$array_need = array();

foreach($string_array_1 as $array_values)
{
        if($array_values)
        {
            $folders =  explode("/", $array_values);
            $array_need[$folders[0]][$folders[1]][$folders[2]][] = $folders[3];
        }
    }

print_r($array_need);
于 2012-04-12T12:54:48.560 回答