I want to pass a php array to javascript. I've tried several examples taken form this site, but the rest of my code seem not to recognize them. I think the problem is in the quotes or format of the array.
First: var functionlist defined as below works OK.
<script type="text/javascript">
var functionlist = Array('1','2','3','4','5','6','7','8','9','10','11','12');
//Rest of the code
</script>
Second: var functionlist defined as below works OK.
<script type="text/javascript">
var functionlist=Array("1","2","3","4","5","6","7","8","9","10","11","12");
//Rest of the code
</script>
But the code below is not working, despite the fact that echoing $TransfArray renders something quite similar to the above.
<?php
for ($i = 0; $i <= 12; $i++) {
$OriginalArray[$i] = $i;
}
$TransfArray= "'" . implode("','", $OriginalArray) . "'";
?>
<script type="text/javascript">
var functionlist = Array(<? echo $TransfArray; ?>);
//Rest of the code
</script>
Nor does the code below
<?php
for ($i = 0; $i <= 12; $i++) {
$OriginalArray[$i] = $i;
}
$Original_to_json = json_encode($OriginalArray);
?>
<script type="text/javascript">
var functionlist = <?php echo $Original_to_json; ?>;
//Rest of the code
</script>
Does anyone detect the problem? Thnaks in advance.