transferring PHP-array to javascript-array

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
Vantuykom
Forum Newbie
Posts: 19
Joined: Fri Jul 04, 2003 6:22 am

transferring PHP-array to javascript-array

Post by Vantuykom »

Hi,

can anybody tell me how I can transfer a PHP-array to javascript-array?
I tried this:

Code: Select all

<?php 
$myarray = array();
$myarray&#1111;1]= "een";
$myarray&#1111;2]= "twee";
$myarray&#1111;3]= "drie";
?>
<SCRIPT LANGUAGE="JavaScript">
for (var i=1;i<= 3;i++)
&#123;
  optTest&#1111;i] = "<?php echo $myarray&#1111;'i']; ?>";
&#125;
</script>
I also tried to set an $i variable between the script-tags, like this:

Code: Select all

<?php $i=1; ?>
for (var i=1;i<= 3;i++)
&#123;
  optTest&#1111;i] = "<?php echo $myarray&#1111;'i']; ?>";
  <?php $i++; ?>
&#125;
but that didn't work eather.

Can anyone please help me???
Vantuykom
Forum Newbie
Posts: 19
Joined: Fri Jul 04, 2003 6:22 am

Post by Vantuykom »

I have found something, but I would like to make a php-function of it:

Code: Select all

function make_js_array($jsArr, $php_array)
// Create JS Array $jsArr = javascript array name, $php_array is array to convert
&#123;
	echo "<script type="text/javascript">\r\n"; 
	echo "jsArr = new Array();\r\n"; 
	foreach($php_array as $key => $value)
	&#123; 
		echo "jsArr&#1111;&#123;$key&#125;] = "&#123;$value&#125;";\r\n"; 
	&#125;
	echo "</script>\r\n";
&#125;
this is working so far, but I would like to make the JS-array-name also a variable, because I want to use the function several times in my page, each time with another name for the JS-array.

Any ideas?
php-programmer
Forum Newbie
Posts: 1
Joined: Tue Feb 03, 2004 9:33 am
Location: Cyprus

Post by php-programmer »

function make_js_array($jsArr, $php_array)
// Create JS Array $jsArr = javascript array name, $php_array is array to convert
{
echo "<script type=\"text/javascript\">\r\n";
echo "jsArr = new Array();\r\n";
foreach($php_array as $key => $value)
{
echo "jsArr[{$key}] = \"{$value}\";\r\n";
}
echo "</script>\r\n";
}
This is really quite simple. The entire content of my test php script follows, including the corrected make_js_array().

The most important thing to understand is where your code is executing. PHP code executes in the web server, before your browser even gets the HTML/Javascript file to display. Javascript executes in the browser, and the browser doesn't even know that the php code exists.

Here's my php test script:
<html>
<head>
<?php
$php_array1 = array("a","b","c");
$php_array2 = array("x","y","z");

function make_js_array($jsArr, $php_array)
// Create JS Array $jsArr = javascript array name, $php_array is array to convert
{
echo "<script type=\"text/javascript\">\r\n";
echo "$jsArr = new Array();\r\n";
foreach($php_array as $key => $value)
{
echo $jsArr."[{$key}] = \"{$value}\";\r\n";
}
echo "</script>\r\n";
}

make_js_array("first",$php_array1);
make_js_array("second",$php_array2);

?>
</head>
<body bgcolor="#FFFFFF">
<button onClick="alert(first[0]+first[1]+first[2]);">
Show First jsArray
</button>
<button onClick="alert(second[0]+second[1]+second[2]);">
Show Second jsArray
</button>
</body>
</html>
Vantuykom
Forum Newbie
Posts: 19
Joined: Fri Jul 04, 2003 6:22 am

Post by Vantuykom »

thanks a lot :D
Post Reply