Assign PHP Array to Javascript Array

Downloads:16744 Bookmark this code Bookmark and Share
Rank : 3.87/5
Rated by : 15 user(s)
Developer:Andrew R  |   13 submission(s)
Date Uploaded:November 21,2007
Level:Begginer
Size: 0 Bytes
Category:PHP -> PHP Basics
Developer Says:

This little code shows you how to assign PHP array to javascript array

<?
	$a[]="one";
	$a[]="two";
	$a[]="three";
	$a[]="four";			
 ?>
<html>
<head>
<title>Assign PHP Array to Javascript Array</title>
<script language="javascript"> 
	function showValues(){
		var a=new Array;
	<?
		for($i=0;$i<count($a); $i++){
			echo "a[$i]='".$a[$i]."';\n";
		}			
	 ?>
		for(i=0;i<a.length;i++)
			alert(a[i]);
	}
 </script>
</head>

<body onload="showValues()">
</body>
</html>


User Reviews
Shyam Sawhney
[04-Jan-2009]
#1

It is a very good. I have faced many times such a problem to convert a php variable in to javascript form

Max
[11-Aug-2009]
#2

THanks it helped

Munim
[08-Apr-2010]
#3

Very nice.. I just wrote a script to convert a two dimensional associative array in php to javascript array!

wapa17
[23-Aug-2010]
#4

Excellent !! A simple, clear and understandable example !

Thank you very much !

Anoop P R
[12-Feb-2011]
#5

Good post.. Its very useful for me...

weeber
[28-Feb-2011]
#6

Excellent!!!

abhijit
[22-May-2011]
#7

good tutorial . thank you ......

mustak
[01-Aug-2011]
#8

very nice

i also need vice versa!!!!

pls help me php to jscript

Vitaquosus
[03-Oct-2011]
#9

This is excellent, I have been looking for this for a while. THX it works great

Leave a Comment
Age (Required, will not be shown)
Name
Email (Required, will not be shown)
Website (Optional, starting with http://)
 
Rate this code
Poor   1 2 3 4 5   Outstanding
Are you human ?

Enter the code shown above