title: How to create associative array in Yii2 and convert to JSON?
body: <p>I am using a calendar in my project and I want to pass data from my <code>Event</code> model to view file in <code>JSON</code> format. I tried following but it didn't work and am not able to display the data properly</p>
  
  <pre><code>$events = Event::find()-&gt;where(1)-&gt;all();
  
  $data = [];
  
  foreach ($events AS $model){
      //Testing
      $data['title'] = $time-&gt;title;
      $data['date'] = $model-&gt;start_date;
      $data['description'] = $time-&gt;description;
  }
  
  \Yii::$app-&gt;response-&gt;format = 'json';
  echo \yii\helpers\Json::encode($data);
  </code></pre>
  
  <p>But it only returns one model in that <code>$data</code> array, the final data should be in  following format:</p>
  
  <pre><code>[
      {"date": "2013-03-19 17:30:00", "type": "meeting", "title": "Test Last Year" },
      { "date": "2013-03-23 17:30:00", "type": "meeting", "title": "Test Next Year" }
  ]
  </code></pre>