日期:2014-05-16  浏览次数:20373 次

请教个数组的问题
想将php里面一个数组转换成js数组
在php里面 echo json_encode($price) 显示如下:
{"1393257600":{"nums":6,"price":"2820"},"1393344000":{"nums":8,"price":"2820"},"1393430400":{"nums":3,"price":"2820"},"1393516800":{"nums":4,"price":"2820"}}
然后是在js里面
var jsPrice = new Array();
jsPrice = <?php echo json_encode($price); ?> ;
打印显示出来的却是[object Object]



------解决方案--------------------
LZ,你的这个不是数组,是json对象,关于json请看资料
------解决方案--------------------
<?php
//{"1393257600":{"nums":6,"price":"2820"},"1393344000":{"nums":8,"price":"2820"},"1393430400":{"nums":3,"price":"2820"},"1393516800":{"nums":4,"price":"2820"}}
$arr = array('1393257600'=>array('nums'=>6,"price"=>'2820'),
 '1393344000'=>array('nums'=>8,"price"=>'2820'),
 '1393430400'=>array('nums'=>3,"price"=>'2820'),
 '1393516800'=>array('nums'=>4,"price"=>'2820'),
);
$json = json_encode($arr);
?>
<html>
<head> 
<script language="JavaScript" type="text/javascript"> 
var jsPrice = new Array();
jsPrice = <?php echo $json;?> ;
alert(jsPrice['1393257600']['price']);
alert(jsPrice['1393257600']['nums']);
</script> 
</head> 
<body> 

</body> 
</html>