PDA

View Full Version : Php to Javascript?


duniyadnd
01-13-2003, 07:28 AM
How would I convert an array in php, to an array in javascript?

<? for ($i = 0; $i < length($array); $i++)
{
echo $array[$i] = ???
}

The more I go about this, the more confused I get.

Thanks
Duniyadnd

Ökii
01-13-2003, 11:25 AM
$my_array = array("a","b","c","d");
echo '<script type="text/javascript">
<!--
my_javascript_array = new Array(';
$my_array_count = count($my_array);
for($indx = 0; $indx < $my_array_count; $indx++)
{
echo '"'.$my_array[$indx].'"';
if($indx !== ($my_array_count-1))
{
echo ',';
}
}
echo ');
-->
</script>';

boywonder
01-13-2003, 10:58 PM
another way if you wanted to transfer an associative array, say it was called $phparray...

<script language="javascript">
var jsarray = new Array();
<?php foreach($phparray as $k => $v) echo "jsarray['$k'] = '$v'; \n"; ?>
</script>