Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

php - Multidimensional array from string

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

Edit : The number of subfolders in the string are dynamic .. from zero sub folders to 10

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

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

The output of

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

Will be

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

I suppose that's what you need :)


UPDATE

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

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

// 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. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...