0

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' => array('ONE' => array('VAR' => $var_one), 'TWO' => array('VAR' => $var_two), 'THREE' => array( 'VAR' => $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?

0

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' => array(
    'ONE' => array(
      'VAR' => 'blah blah blah', 
      'VAR_ONE' => 'blah blah blah one'
    ), 
    'TWO' => array(
      'VAR' => 'blah blah blah two', 
      'VAR_ONE' => 'blah blah blah two'
    ), 
    'THREE' => array(
      'VAR' => 'blah blah blah three', 
      'VAR_ONE' => '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"}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.