title: Combining Multiple foreach Result in json_encode
body: <p>I am trying to get the following json_encode output:</p>
  
  <p>The Array:</p>
  
  <pre><code>echo json_encode(array(
               array(
               'id' =&gt; 111,
               'title' =&gt; "Event1",
               'start' =&gt; "2012-8-10",
               'end' =&gt; "2012-8-15",
               'url' =&gt; "http://yahoo.com/",
                  ),
              array(
               'id' =&gt; 222,
               'title' =&gt; "Event2",
               'start' =&gt; "2012-8-20",
               'end' =&gt; "2012-8-22",
               'url' =&gt; "http://yahoo.com/"
                 )
                ));
  </code></pre>
  
  <p>OUTPUT:</p>
  
  <pre><code>    [{"id":111,"title":"Event1","start":"2012-8-10","end":"2012-8-15","url":"http:\/\/yahoo.com\/"},{"id":222,"title":"Event2","start":"2012-08-20","end":"2012-08-22","url":"http:\/\/yahoo.com\/"}]
  </code></pre>
  
  <p>However, when I am using the following code</p>
  
  <pre><code>global $wpdb;
  $row = $wpdb-&gt;get_results("SELECT $wpdb-&gt;posts.ID, $wpdb-&gt;posts.post_title FROM $wpdb-&gt;posts WHERE $wpdb-&gt;posts.post_type = 'calendar' AND $wpdb-&gt;posts.post_status = 'publish' ORDER BY $wpdb-&gt;posts.post_date ASC");
  foreach ($row as $post) {
      $postid = $post-&gt;ID;
      $post_title = $post-&gt;post_title;
      $startDate =  get_post_meta($post-&gt;ID,'calendar_start-date',true);
      $endDate =  get_post_meta($post-&gt;ID,'calendar_end-date',true);
      $link =  get_post_meta($post-&gt;ID,'calendar_link',true);
      $arr = array('id' =&gt; $postid, 'title' =&gt; $post_title, 'start' =&gt; $startDate, 'end' =&gt; $endDate, 'url' =&gt; $link);
      echo json_encode($arr);
      }
  </code></pre>
  
  <p>I get the output as</p>
  
  <pre><code>{"id":"320","title":"Test Event One","start":"2012-8-17","end":"2012-8-24","url":"http:\/\/www.yahoo.com"}{"id":"321","title":"Test Event Two","start":"2012-8-21","end":"2012-8-30","url":"http:\/\/www.google.com"}
  </code></pre>
  
  <p>I understand it is probably due to the json_encode being inside the foreach loop. However when I try it outside, it only shows the first result. How to combine these foreach values to achieve the output as mentioned above?</p>