1,105,636 Community Members

PHP string to nested / multidimensional array

Member Avatar
omnia456
Newbie Poster
3 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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,

Member Avatar
pritaeas
mod_pritaeas
11,316 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
0
 

If you split your string on the @, you can loop through each item and parse it correctly into your array.

Member Avatar
omnia456
Newbie Poster
3 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

If you split your string on the @, you can loop through each item and parse it correctly into your array.

unfortunately I can not change the string as it comes from a Windows program.

it is not formatted for php but there is a kind of rustic formatting.

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] [green] = no
* [Blue] [movie] = 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.

the problem is with the recursion!

something like that..

is it clear now? thank you

Member Avatar
pritaeas
mod_pritaeas
11,316 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
0
 

There is no recursion in your logic. What code do you have so far ?

Member Avatar
omnia456
Newbie Poster
3 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

There is no recursion in your logic. What code do you have so far ?

i tried with recursion but i dind't get it..

Member Avatar
pritaeas
mod_pritaeas
11,316 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
0
 

Your code does not need recursion. Can't help if you don't share your code.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: