Page 1 of 1

PHP array to javascript Array

Posted: Tue Jan 19, 2010 5:12 am
by waseem83
Can any body tell me simple two lines code for converting PHP array to javascript array.
$arr=array("a","b","c");
I have to pass array as argument like onclick="myFunction('<? echo $arr;?>')".

<script language="javascript">
function myFunction(jsArray)
{
I want to show "a","b","c" here.
}
</script>

Can any body tell me how should i do?

Re: PHP array to javascript Array

Posted: Tue Jan 19, 2010 8:53 am
by AbraCadaver
json_encode()

Re: PHP array to javascript Array

Posted: Tue Jan 19, 2010 10:52 pm
by waseem83
I didnt find any thing there to convert PHP array to javascript array there.

Re: PHP array to javascript Array

Posted: Wed Jan 20, 2010 7:50 am
by AbraCadaver
Well, that's exactly what it's for.

Re: PHP array to javascript Array

Posted: Thu Jan 28, 2010 6:46 am
by aneesme

Code: Select all

<?php
$arr = array("a","b","c");
?>
<script>
var jsArray = ["<?php echo join("\", \"", $arr); ?>"]; 
alert(jsArray[0]);
</script>
Probably set the jsArray as a global variable and refer it in your function.