title: Sending array from PHP to javascript through ajax
body: <p>I have this php code to return many arrays to javascript through ajax.</p>
  
  <pre><code>&lt;?php
  
  ini_set('display_errors', 1);
  error_reporting(E_ALL);
  
  function get_events_data() {
  
      // load SimpleXML
      $nodes = new SimpleXMLElement('my_events.xml', null, true);
  
      $event_id = array();
      $channel_id = array();
      $channel_name = array();
      $channel_onclick = array();
      $event_site = array();
      $event_url = array();
      $start_date = array();
      $start_time = array();
      $end_date = array();
      $end_time = array();
      $event_notes = array();
  
      $n = 0;
      foreach($nodes as $node) 
      {       
  
          $event_id[$n] = $node['id'];               
          $channel_id[$n] = $node-&gt;channel['id'];
          $channel_name[$n] = $node-&gt;channel-&gt;name;
          $channel_onclick[$n] = $node-&gt;channel-&gt;onclick;
          $event_site[$n] = $node-&gt;event_site-&gt;name;
          $event_url[$n] = $node-&gt;event_site-&gt;url;
          $start_date[$n] = $node-&gt;start_date;
          $start_time[$n] = $node-&gt;start_time;
          $end_date[$n] = $node-&gt;end_date;
          $end_time[$n] = $node-&gt;end_time;
          $event_notes[$n] = $node-&gt;notes;
          $n++;
      }
  
      $return['event_id'] = $event_id;               
      $return['channel_id'] = $channel_id;
      $return['channel_name'] = $channel_name;
      $return['channel_onclick'] = $channel_onclick;
      $return['event_site'] = $event_site;
      $return['event_url'] = $event_url;
      $return['start_date'] = $start_date;
      $return['start_time'] = $start_time;
      $return['end_date'] = $end_date;
      $return['end_time'] = $end_time;
      $return['event_notes'] = $event_notes;
  
      echo json_encode($return);
  
  }
  
  echo get_events_data();
  
  ?&gt;
  </code></pre>
  
  <p>On the javascript side I have this code to access the arrays.</p>
  
  <pre><code>    $.ajax({
          url: "get_events_data.php",
          type: "POST",
          dataType : 'json',
          data: { },
          cache: false,
          async: false,
          success: function (rdata) {
              var alert_data = 'event_id '+rdata.event_id[0]+'&lt;br/&gt;'+    
              'channel_id '+rdata.channel_id[0]+'&lt;br/&gt;'+
              'channel_name '+rdata.channel_name[0]+'&lt;br/&gt;'+
              'channel_onclick '+rdata.channel_onclick[0]+'&lt;br/&gt;'+
              'event_site '+rdata.event_site[0]+'&lt;br/&gt;'+
              'event_url '+rdata.event_url[0]+'&lt;br/&gt;'+
              'start_date '+rdata.start_date[0]+'&lt;br/&gt;'+
              'start_time '+rdata.start_time[0]+'&lt;br/&gt;'+
              'end_date '+rdata.end_date[0]+'&lt;br/&gt;'+
              'end_time '+rdata.end_time[0]+'&lt;br/&gt;'+
              'event_notes '+rdata.event_notes[0]+'&lt;br/&gt;';
              alert (alert_data);
          },
          error: function (request, status, error) {
              alert ("status "+status+" error "+error+"responseText "+request.responseText);
          },
  });
  </code></pre>
  
  <p>When I print out the first element of each array on the javascript side, it shows as "object" doesn't show the value in that array.  What is the right way of accessing the array data on the javascript side?</p>