...

View Full Version : Php array into a javascript array



alex57
11-20-2006, 11:35 AM
Hello,

I have php array and I need to transfer this array into a javascript array. Does anyone have any code as an example of how this is done??

thanks

SeeIT Solutions
11-20-2006, 11:42 AM
<script language="Javascript">
var phparray = array(<?php echo implode(',',$array); ?>);
</script>

Try that.

If you need to keep key values use this


<script language="Javascript">
var phparray = array();

<?php
foreach($array as $key => $value) {
echo "phparray[".$key."] = '".$value."';
";
}
</script>

GJay
11-20-2006, 01:20 PM
with php 5.2 (or with the extension installed in earlier versions):


$array=array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

echo json_encode($array);
//output: {"a":1,"b":2,"c":3,"d":4,"e":5}

http://php.net/json-encode



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum