我有一个 javascript 动态数组创建者代码,如下所示
function createArray() {
var myArr = new Object();
myArr[0] = createSubArray('apple');
myArr[1] = createSubArray('Mango');
myArr[2] = createSubArray('Pineapple');
myArr[3] = createSubArray('Grape');
document.getElementById("a").innerHTML = JSON.stringify(myArr);
return myArr;
}
function createSubArray(name){
var arr = new Object();
elems = document.getElementsByName(name);
for (var i=0;i<elems.length;i++){
if (elems[i].checked){
arr[name] = elems[i].value;
arr['price'] = elems[i].getAttribute('data-price');
}
}
return arr;
}
在此代码中,我需要将值作为array
not 发送object
。我尝试将函数更改new Object();
为new Array();
in , createArray
但是当json_decode
我得到如下结果时
array
0 =>
object(stdClass)[1]
public 'apple' => string 'light' (length=5)
public 'price' => string '10' (length=2)
1 =>
object(stdClass)[2]
public 'Mango' => string 'light' (length=5)
public 'price' => string '30' (length=2)
2 =>
object(stdClass)[3]
public 'Pineapple' => string 'light' (length=5)
public 'price' => string '50' (length=2)
3 =>
object(stdClass)[4]
public 'Grape' => string 'dark' (length=4)
public 'price' => string '80' (length=2)
如果我在 createSubArray 函数中更改新对象,我不会得到任何值.. 请检查下面
new Object 在 createArray 和 createSubArray 函数中改为 new Array
我如何将对象更改为数组?
请帮我解决这个问题