Add array in array recursively?

Discussion in 'PHP' started by nljeffrey, Oct 26, 2007.

  1. #1
    I am totally stuck! I've been trying all day to make the following work:

    I have a POST collection that contains some inputboxes. These inputboxes have a name like: root|element1|subelement1

    I split this name into an array list and that gives me:
    $elements[0] = root
    $elements[1] = element1
    etc

    Now I want to create an array list that looks like this from the $elements list:

    Array
    (
    [root] => Array
    (
    [element1] => Array
    (
    [subelement1] => inputbox value
    )

    )

    )

    Basically, what I want to do is:
    $arrTest['root']['element1']['subelement1'] = 'inputbox value';

    I can't seem to create an array dynamically. I need to do it dynamically because I don't know how many elements will be returned from the inputbox name.

    Hope this explains it and someone can help me...
     
    nljeffrey, Oct 26, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    The trick does actually lay in your HTML form.
    
    <input type="text" name="root[element][subelement]" />
    
    HTML:
     
    nico_swd, Oct 26, 2007 IP
  3. nljeffrey

    nljeffrey Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yep that does the trick!!!
    Can't thank you enough for saving my day :)

    Thanks!
     
    nljeffrey, Oct 26, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    You're welcome. Glad I could help. :)
     
    nico_swd, Oct 26, 2007 IP