PHP array to javascript Array

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
waseem83
Forum Commoner
Posts: 54
Joined: Tue Jun 23, 2009 3:51 am
Contact:

PHP array to javascript Array

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP array to javascript Array

Post by AbraCadaver »

json_encode()
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
waseem83
Forum Commoner
Posts: 54
Joined: Tue Jun 23, 2009 3:51 am
Contact:

Re: PHP array to javascript Array

Post by waseem83 »

I didnt find any thing there to convert PHP array to javascript array there.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP array to javascript Array

Post by AbraCadaver »

Well, that's exactly what it's for.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
aneesme
Forum Newbie
Posts: 9
Joined: Wed Jan 27, 2010 4:22 am
Location: Bangalore

Re: PHP array to javascript Array

Post 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.
Post Reply