Jump to content

PHP string to nested / multidimensional array


omnia456

Recommended Posts

I have this example php string:

 

$string = "@[item_1][door] @[mozart][grass] = yes @[mozart][green] = no @[mozart][human] @[blue][movie]=yes @[item_1][beat] = yes @[item_1][music] = no ";

 

now $string idented just to easy view:

 

@[item_1][door] 
    @[mozart][grass] = yes 
    @[mozart][green] = no 
    @[mozart][human] 

         @[blue][movie]=yes

@[item_1][beat] = yes 
@[item_1][music] = no

 

I want to know how can i get this string ( or other string following this style ) and transform in an array that looks like:

 

Array
(
    [item_1] => Array
        (
            [door] => Array
                (
                    [mozart] => Array
                        (
                            [grass] => yes
                            [green] => no
                            [human] => Array
                                (
                                    [blue] => Array
                                        (
                                            [movie] => yes
                                        )
                                )
                        )
                )
            [beat] => yes
            [music] => no
        )
)

 

What i tried

 

I tried to use and recursive function to create an nested array but i can't have access to the array pointer ( in deep levels ) in recursive functions.. don't know why.. maybe is the wrong patch to the answer. thank you,

Link to comment
Share on other sites

Hello,

 

thank you for the attention.

 

this string comes from a windows application.. so it has to be this may.

 

i tried using explode:

I thought that an algorithm should work, but I can not make it reality:

 

1) explode the string @ this will give me two types of pieces:

 

a) Arrays as pieces that are:

* [item_1] [door]

* [Mozart] [human]

b) pieces that have the "=" in them are then:

* [Mozart] [grass] = yes

* [Mozart] = no

* [blue] = yes

* [item_1] [beat] = yes

* [item_1] [music] = no

 

2) the algorithm:

 

1) create an empty array

2) foreach in the string piactes

if piace_are_array and item_does_not_exist($tree) -> add_current_piece

else if attibrute_existe -> append on it

 

if is_not_array -> append in the last attribute added.

 

something like that..

 

is it clear now? thank you

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.