Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation PHP string to nested / multidimensional array

    I have this example php string:

    PHP Code:
    $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:


    Code:
    @[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:
    PHP Code:
    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,
    Last edited by omnia456; 12-30-2011 at 02:06 PM.

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Where is this string coming from? It's not really formatted to any relevant pattern for PHP to translate. For example, there's no real relationships defined in the string - it doesn't tell us what is a child of what, or any sort of delimiter (the bottom of the array) that suggests the style of array you want.

    To me, your array in the string looks more like this:
    PHP Code:
    Array(
        
    'item_1' => Array(
                            
    // @[item_1][door] @[item_1][beat] = yes @[item_1][music] = no
                            
    'door' => '',
                            
    'beat' => 'yes',
                            
    'music' => 'no'
                       
    ),
        
    'mozart' => Array(
                            
    // @[mozart][grass] = yes @[mozart][green] = no @[mozart][human]
                            
    'grass' => 'yes',
                            
    'green' => 'no',
                            
    'human' => ''
                        
    ),
        
    'blue' => Array(
                        
    // @[blue][movie]=yes
                        
    'movie' 'yes'
                    
    )

    Which is completely different. If you're trying to pass PHP arrays between pages, you can use the serialize() function.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #3
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb

    hello,
    Thank you for your attention.

    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.

    something like that..

    is it clear now? thank you


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •