从字符串多维数组 [英] Multidimensional array from string

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

问题描述

让我们说我有这个字符串,我想提出一个多维数组。

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";
?>

我想下面的返回数组

I want the following array returned

<?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, 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);

P.S。
为了这个数组转换为字符串,只需调用 json_en code ,但我认为没有理由将其转换为一个数组,然后返回到它是什么。

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天全站免登陆