public function actionExport($url = false) { $this->type = 'export'; if ($url) { $model = new Webmeup(); Yii::$app->session->set('url', $url); $array = json_decode($model->export($url), true); $f = fopen('php://output', 'w'); $firstLineKeys = false; foreach ($array as $line) { if (empty($firstLineKeys)) { $firstLineKeys = array_keys($line); fputcsv($f, $firstLineKeys); $firstLineKeys = array_flip($firstLineKeys); } // Using array_merge is important to maintain the order of keys acording to the first element fputcsv($f, array_merge($firstLineKeys, $line)); } echo $f; Yii::$app->end(); } return $this->render('empty_main'); }