嗨最初我的阵列看起来是这样的多维JSON编码关联数组的PHP JS
PHP
$results = array(
"banana" => $bananavalue,
"apple" => $applevalue,
);
echo json_encode($results);
JS
var fruits = [];
$.ajax({
type: "POST",
url: "actions/MYphp.php",
data: PassArray,
dataType: 'json',
beforeSend: function (html) {
// alert(html);
},
success: function (html) {
var obj = html;
// Now the two will work
$.each(obj, function (key, value) {
fruits.push([key, value]);
});
不过,我想将其更改为一个多维的水果和蔬菜如下:
results = array(
"fruit"=>array(
"banana" => $bananavalue,
"apple" => $applevalue
),
"vegetables"=>array(
"lettuce" => $lettuce,
"cabbage" => $cabbage
)
);
echo json_encode($results);
的问题是我在Javascript中每个阵列中循环,以及如何将其分配给两个数组(水果和蔬菜)
我已经试过
$.each(obj['fruit'], function(key, value) {
fruits.push([key, value]);
});
但没有奏效。
当你说它没有工作,它以什么方式不起作用? – 2013-03-11 23:20:52
PHP关联数组json-decode到javascript对象。 – 2013-03-11 23:24:28