Push Items to Array using drupal_add_js I'm using this code in a for loop to push items to an array but it doesn't add to the array, just overwrites it each time I think:

drupal_add_js(array('THINGS' =&gt; array('ONE' =&gt; array('VAR' =&gt; $var_one), 'TWO' =&gt; array('VAR' =&gt; $var_two), 'THREE' =&gt; array( 'VAR' =&gt; $var_three))), 'setting');


So this is what I end up with:

"THINGS":{"ONE":{"VAR":"blah blah blah"},"TWO":{"VAR":"blah blah blah two"},"THREE":{"VAR":"blah blah blah three"}}


When what I want is this:

"THINGS":{"ONE":{"VAR":"blah blah blah", "VAR":"blah blah blah one"},"TWO":{"VAR":"blah blah blah two","VAR":"blah blah blah two two"},"THREE":{"VAR":"blah blah blah three","VAR":"blah blah blah three three"}}


So I guess my question is, how do I print objects within objects using this method, or would it be easier to just push the variables with javascript within a script tag?

Any ideas? Am I doing this right?
The problem seems to be the key of the nested array, where you are using 'VAR', you are repeating this key and that makes PHP to override the value on the array (or on the object on Js).

I think you would have to use the 'VAR' key on a slightly different way:

drupal_add_js(array('THINGS' =&gt; array(
    'ONE' =&gt; array(
      'VAR' =&gt; 'blah blah blah', 
      'VAR_ONE' =&gt; 'blah blah blah one'
    ), 
    'TWO' =&gt; array(
      'VAR' =&gt; 'blah blah blah two', 
      'VAR_ONE' =&gt; 'blah blah blah two'
    ), 
    'THREE' =&gt; array(
      'VAR' =&gt; 'blah blah blah three', 
      'VAR_ONE' =&gt; 'blah blah blah three three'
    )
  )
));


to get what you are looking:

"THINGS":{"ONE":{"VAR":"blah blah blah", "VAR":"blah blah blah one"},"TWO":{"VAR":"blah blah blah two","VAR":"blah blah blah two two"},"THREE":{"VAR":"blah blah blah three","VAR":"blah blah blah three three"}}