来自字符串的多维数组 [英] Multidimensional array from string

查看:36
本文介绍了来自字符串的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个想要放入多维数组的字符串.

Let's say I have this string which I want to put in a multidimensional array.

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

Edit : The number of subfolders in the string are dynamic .. from zero sub folders to 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
                        )

                )

        )

)
?>

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

What would be the most efficient way to accomplish this ?

为了好玩,假设这个数组将被发送到世界的另一端,并且它想要返回一个字符串.我们该怎么做?

And for the fun of it let's say this array will be send to the other side of the world and it wants to return to a string. How would we do that ?

推荐答案

你可以从这个类(链接不再可用)借用代码片段,特别是_processContentEntry方法.

You could borrow pieces of code from this class (link no longer available), specifically the _processContentEntry method.

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

Here's a modified version of the method that does the job:

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"
      }
    }
  }
}

我想这就是你所需要的:)

I suppose that's what you need :)

更新

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

As per your comment, here's how you can process a string separated by new line characters:

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

附言要将这个数组转换为字符串,只需调用 json_encode,但是我认为没有理由将其转换为数组,然后再恢复原状.

P.S. To convert this array to a string, just call json_encode, however I see no reason to convert it to an array and then back to what it was.

这篇关于来自字符串的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆