嗨,最初我的数组看起来像这样
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]);
});
但这没有用。