1

I am trying to turn a string into a nested array
Here is my string:

a/b/d.docx

and I wanted to be like this:

array(
    "name" => "a",
    "type" => "folder",
    "sub" => array(
        "name" => "b",
        "type" => "folder",
        "sub" => array(
            "name" => "c.docx",
            "type" => "file",
            "size" => "20"
        )
    )
)

This is the code that I have so far

$items = explode('/', $strings);
$num = count($items);
$num = --$num; 
$temp = array();
foreach($items as $keys => $value) {
    $temp[$keys] = array(
        "name" => $value,
        "type" => "folder",
        "items" => $temp[++$keys]
    );
    if($keys == $num){
        $temp[$keys] = array(
            "name" => $value,
            "type" => "file",
            "size" => "20"
        ); 
    }
}
var_dump($temp);

I am trying this functions but this only turn string into a single array and it also can't do the 'items' line.
Any help would be appreciated.Thanks.
Note that the path is virtual and doesn't exist.
UPDATE: How can I add path to each array??for example,"path"=>"a/b"

2
  • You're going to have comment a little, I can't follow what you are doing and what you are trying to do. Commented Mar 20, 2017 at 15:06
  • $num = --$num is useless, you can use --$num it will be the same Commented Mar 20, 2017 at 15:25

3 Answers 3

2

You can do that:

$path = 'a/b/d.docx';

$parts = explode('/', $path);

$result = [ 'name' => array_pop($parts), 'type' => 'file', 'size' => 20 ];

while ($parts) {
    $result = [ 'name' => array_pop($parts), 'type' => 'folder', 'sub' => $result ];
}

print_r($result);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help.How can I add path to arrays?for example "path"=> "a/b"
0
<?php
$strings='a/b/d.docx';

$items = explode('/', $strings);
$num = count($items)-1;

$root= array();
$cur = &$root;

$v='';

foreach($items as $keys => $value) {

    $v = $v.$value;
    $temp = array(   "name" => $value,  "path"=>$v,  "type" => "folder",    "items" => "");
    if($keys == $num){ 
        $temp = array( "name" => $value, "path"=>$v, "type" => "file", "size" => "20"); 
    }
    $v= $v.'/';

    if($keys==0) {
        $cur = $temp;
    }
    else
    {
        $cur['items'] = $temp;      
        $cur = &$cur['items'];
    }
}
var_dump($root);

6 Comments

Thanks for your help.How can I add path to arrays?for example "path"=> "a/b"
I edited my answer post, the answer is easy to understand, you have to try by yourself to learn something
Can you please explain the items part?
The core idea is recursion, Casimir et Hippolyte's answer is elegant, he pops element from the tail, I use method from the head, and connect each element's items head to tail, it's all.
Thanks for your help. I also have one more question. How can I put the items JSON output in brackets? like: "items":[{ .....
|
0

Try recursion:

public function testAction(){
    $sString = 'a/b/c/d.exe';
    $aExploded = explode('/', $sString);
    var_dump($this->_parse_folder_rec($aExploded));
}

private function _parse_folder_rec($aExploded){
    $aResult = [];
    $aResult['name'] = array_shift($aExploded);
    if($aExploded){
        $aResult['type'] = 'folder';
        $aResult['sub'] = $this->_parse_folder_rec($aExploded);
    }else{
        $aResult['type'] = 'file';
        $aResult['size'] = 20;
    }
    return $aResult;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.