Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7024531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:51:07+00:00 2026-05-27T23:51:07+00:00

I have this example php string: $string = @[item_1][door] @[mozart][grass] = yes @[mozart][green] =

  • 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:

  1. @[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,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:51:08+00:00Added an answer on May 27, 2026 at 11:51 pm

    OK, I hope you still need this, because I wasted more time than I’d like to admin getting this right 🙂

    Basically, my approach was to first manipulate the string into the format [set][of][keys]=value, and then loop through the string of keys and comparing them with the last set of keys to create the correct key hierarchy. I used eval because it’s easier, but you can write a replacement function if you can’t stomach seeing that function in your code:

    //FIRST WE GET THE STRING INTO EASIER TO WORK WITH CHUNKS
    $original_string = "@[item_1][door] @[mozart][grass] = yes @[mozart][green] = no @[mozart][human] @[blue][movie]=yes @[item_1][beat] = yes @[item_1][music] = no ";
    $cleaned_string = str_replace('] @[','][',$original_string);
    /* This results in clusters of keys that equal a value:
    @[item_1][door][mozart][grass] = yes @[mozart][green] = no @[mozart][human][blue][movie]=yes @[item_1][beat] = yes @[item_1][music] = no 
    
    OR (with line breaks for clarity):
    
    @[item_1][door][mozart][grass] = yes 
    @[mozart][green] = no 
    @[mozart][human][blue][movie]=yes 
    @[item_1][beat] = yes 
    @[item_1][music] = no */
    
    //break it up into an array:
    $elements = explode('@',$cleaned_string);
    
    //create a variable to compare the last string to
    $last_keys = "";
    //and another that will serve as our final array
    $array_of_arrays = array();
    //now loop through each [item_1][door][mozart][grass] = yes,[mozart][green] = no, etc
    foreach($elements as $element){
        if ($element==""){continue;} //skip the first empty item
    
        //break the string into [0] = group of keys and [1] the value that terminates the string 
        //so [item_1][door][mozart][grass] = yes BECOMES [item_1][door][mozart][grass], AND yes
        $pieces = explode('=',str_replace(array('[',']'),array("['","']"),trim($element))); 
        //now compare this set of keys to the last set of keys, and if they overlap merge them into a single key string
        $clean_keys = combine_key_strings($pieces[0],$last_keys);
        //set the new key string the value for the next comparison
        $last_keys = $clean_keys;
        //and (ugly, I know) we use an eval to convert "[item_1][door][mozart][grass]='yes'" into a properly keyed array
        eval("\$array_of_arrays".$clean_keys." = '".trim($pieces[1])."';");
    }
    
    //now dump the contents
    print_r($array_of_arrays);
    
    
    //THIS FUNCTION COMPA
    function combine_key_strings($new,$old){
        //get the key that starts the newer string
        $new_keys = explode('][',$new);
        $first_key = $new_keys[0].']';
    
        //see if it appears in the last string
        $last_occurance = strrpos ($old,$first_key);
        //if so, merge the two strings to create the full array keystring
        if (is_int($last_occurance)){
            return substr($old,0,$last_occurance).$new;
        }
        return $new;
    }
    

    This should spit out your correctly nested array:

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

    Good night!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

similiar like this example, php: remove brackets/contents from a string? i have no idea
I have looked at this example using php and GD to piecewise-render a spiral
Let's say I have this url: www.example.com/test.php?page=4 How do i retrieve the value of
i would like a simple help... i have a url like this: example.com/profile.php?id= &
I have a PHP array which looks like this example: $array[0][0] = 'apples'; $array[0][1]
For example I have this string of 2 json objects: {"glossary": {"title": "example glossary"},
I have this URL: oldsite.example/profile.php?uid=10 I would like to rewrite it to: newsite.example/utenti/10 How
I have a PHP String like this: $string = 'a => 1 , b
I have the following JSON string, encoded with PHP 5.2 json_encode(): {foo:\\.} This JSON
Here is what I am trying to achieve in PHP: I have this string:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.