In Yii you can use registerJS in your view file:
<?php
$js = "var states = ['" .
implode("','", ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California']) .
"'];";
$this->registerJs($js, View::POS_BEGIN)
// Now states is globally available in JS
?>
<div> ... your view html ... </div>
You can also add this JS code within a controller action instead of placing it into the view file:
public function actionIndex() {
$js = "var states = ['" .
implode("','", ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California']) .
"'];";
$this->view->registerJs($js, View::POS_BEGIN);
return $this->render('index', []);
}
Of course you can replace the array with any array you want. You could use e.g.:
$states = States::find()->column();