foreach in Yii::app()->clientScript->registerScript

hello everyone, i have a little problem that i not found a solution, i want to pass a php array to javascript and i have this example code




<?php

$arreglo = array(

'clave 1' => 'valor 1',

'clave 2' => 'valor 2',

'clave x' => 'valor x',

);

?>

<script language="javascript">

var arr = {

<?php

foreach($arreglo as $key => $values){

$arr .= "'" . $key . "'" .   " : " . "'" . $values . "'" . ",";

}

$arr =  substr_replace($arr, '', -1);

echo $arr;

?>

};

</script>



that it works perfect, but when i try to use the same php code with or inside the Yii::app()->clientScript->registerScript like this




       '".$arreglo = $this->valida_muelle_carga($model_bus->PLACA)."';


	var listado = {

	'".

	foreach($arreglo as $key => $values){

	$arr .= "'" . $key . "'" .   " : " . "'" . $values . "'" . ",";

	}

	$arr =  substr_replace($arr, '', -1);

	echo $arr;

	."'

	};



i have a syntax error unexpected foreach, i want to know that if is possible to use this type of php function in the Yii::app()->clientScript->registerScript or my syntax is very wrong




<?php

$arreglo = $this->valida_muelle_carga($model_bus->PLACA);


$script = 'var listado = ' . CJSON::encode($arregio) . ';';

Yii::app()->clientScript->registerScript('script-id', $script);

?>



CJSON::encode() returns JSON but JSON syntax is also valid javascript syntax.

Second parameter of CClientScript::registerScript() is a simple string. It won’t be evaluated as PHP code.