来自字符串的多维数组

时间:2012-04-12 12:30:02

标签: php arrays multidimensional-array

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

编辑:字符串中子文件夹的数量是动态的..从零子文件夹到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
                        )

                )

        )

)
?>

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

为了它的乐趣,让我们说这个数组将被发送到世界的另一端,它想要返回一个字符串。我们该怎么做?

3 个答案:

答案 0 :(得分:9)

您可以从this class借用一些代码,特别是_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);

P.S。 要将此数组转换为字符串,只需调用json_encode,但我认为没有理由将其转换为数组,然后再将其转换为数组。

答案 1 :(得分: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 );

答案 2 :(得分:-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);