Adding array of objects to javascript from PHP This is a part of my script in template file where php code is used to generate js field of objects:

&lt;?php $count = count($events); $counter = 1; ?&gt;

&lt;script&gt;
  var events = [

  &lt;?php foreach($events as $event) { ?&gt;

    {
      date: '&lt;?php echo $event['date']; ?&gt;',
      linkTarget: "_blank",
      content: "&lt;?php echo $event['content']; ?&gt;",
      class: "&lt;?php echo $event['class']; ?&gt;"
    }
    &lt;?php if($counter!=$count) { echo ','; $counter++; } ?&gt;
  &lt;?php } ?&gt;

  ];
&lt;script&gt;


I know that we can add js variable by drupal_add_js(array('myModule' =&gt; array('key' =&gt; 'value')), 'setting'); but how can we create the same js array as in example above in order not to use php code in js?
You can pass an array to JS like this:

drupal_add_js(array('my_module' =&gt; array('events' =&gt; $events)), 'setting');

In javascript you can get the values like this:

Drupal.settings.my_module.events;