Assign PHP Array to Javascript Array

Downloads:7611 Bookmark this code Bookmark and Share
Rank : 3.57/5
Rated by : 7 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>

The above code was highlighted with Neat Highlighter


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!

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