View Full Version : Php array into a javascript array

11-20-2006, 11:35 AM

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??


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

Try that.

If you need to keep key values use this

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

foreach($array as $key => $value) {
echo "phparray[".$key."] = '".$value."';

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}


EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum