使用JSON.stringify时不需要的数组索引

时间:2022-11-20 15:12:27

I use JSON.stringify method to pass an array to server.

我使用JSON.stringify方法将数组传递给服务器。

I have an array that has 4 elements:

我有一个有4个元素的数组:

arr[10] = 1;
arr[20] = 1;
arr[30] = 1;
arr[40] = 1;

Then i do this:

然后我这样做:

arr = JSON.stringify(arr);

Then send it to the server:

然后将其发送到服务器:

                jQuery.ajax({
                    type: "post",
                    url: baseurl+"profile/mprofile/action/ratings/add_ratings",
                    data:{"checkbox":checkbox,"review":review,"speciality":speciality,"arr":arr},
                    success: function(data, status) {  

                        jQuery('#header-error').html(data);
                    } 
                }); 

I am getting the array in PHP with:

我在PHP中获取数组:

    $arr = $this->ci->input->post('arr');
    $arr = json_decode($arr);
    print_r($arr) ; die ;

the result is

结果是

Array
(
    [0] => 
    [1] => 
    [2] => 
    [3] => 
    [4] => 
    [5] => 
    [6] => 
    [7] => 
    [8] => 
    [9] => 
    [10] => 1
    [11] => 
    [12] => 
    [13] => 
    [14] => 
    [15] => 
    [16] => 
    [17] => 
    [18] => 
    [19] => 
    [20] => 1
    [21] => 
    [22] => 
    [23] => 
    [24] => 
    [25] => 
    [26] => 
    [27] => 
    [28] => 
    [29] => 
    [30] => 1
    [31] => 
    [32] => 
    [33] => 
    [34] => 
    [35] => 
    [36] => 
    [37] => 
    [38] => 
    [39] => 
    [40] => 1
)

Why is this happening?

为什么会这样?

3 个解决方案

#1


5  

That's how javascript arrays work. When you set value to an index that is out of range, array is expanded, value is successfully set and all 'missing' values are set to undefined.

这就是javascript数组的工作方式。将值设置为超出范围的索引时,将展开数组,成功设置值并将所有“缺失”值设置为undefined。

You are spoiled by PHP behaviour that blends the difference between arrays and dictionaries (or "associative arrays", in PHP talk).

您被PHP行为所破坏,这种行为混合了数组和字典(或“关联数组”,PHP谈话)之间的差异。

To avoid this behaviour, just create a dictionary in javascript instead of array.

要避免此行为,只需在javascript而不是数组中创建字典。

var arr = {};
arr[10] = 1;
arr[20] = 2;

Also, on PHP side you should pass true as a second parameter to json_decode.

此外,在PHP端,您应该将true作为第二个参数传递给json_decode。

json_decode($arr, true)

This will make it return array instead of stdclass.

这将使它返回数组而不是stdclass。

#2


1  

How do you initialize arr? You probably want to use an object instead of an Array. For example:

你如何初始化arr?您可能希望使用对象而不是Array。例如:

var arr = {};
arr[10] = 1;
arr[20] = 1;
arr[30] = 1;
arr[30] = 1;
var jsonified = JSON.stringify(arr);
console.log(jsonified);

#3


1  

Arrays in JavaScript work a bit differently than in PHP. You don't actually have an array with 4 elements, you have one with 41 elements right from the start. For example, when you say arr[ 10 ] = 1, every array element between 0 and 9 are automatically assigned undefined.

JavaScript中的数组与PHP中的数组略有不同。你实际上没有一个包含4个元素的数组,你从一开始就拥有41个元素。例如,当你说arr [10] = 1时,0到9之间的每个数组元素都会自动分配为undefined。

I suggest you switch the logic around: instead of using array keys to track values, use array values instead:

我建议你切换逻辑:而不是使用数组键来跟踪值,而是使用数组值:

arr = [ 10, 20, 30, 40 ];

Or, if you need other than binary values, use objects:

或者,如果您需要二进制值以外的其他值,请使用对象:

arr = [
    { score: 10, value: 1 },
    { score: 20, value: 5 }
];

#1


5  

That's how javascript arrays work. When you set value to an index that is out of range, array is expanded, value is successfully set and all 'missing' values are set to undefined.

这就是javascript数组的工作方式。将值设置为超出范围的索引时,将展开数组,成功设置值并将所有“缺失”值设置为undefined。

You are spoiled by PHP behaviour that blends the difference between arrays and dictionaries (or "associative arrays", in PHP talk).

您被PHP行为所破坏,这种行为混合了数组和字典(或“关联数组”,PHP谈话)之间的差异。

To avoid this behaviour, just create a dictionary in javascript instead of array.

要避免此行为,只需在javascript而不是数组中创建字典。

var arr = {};
arr[10] = 1;
arr[20] = 2;

Also, on PHP side you should pass true as a second parameter to json_decode.

此外,在PHP端,您应该将true作为第二个参数传递给json_decode。

json_decode($arr, true)

This will make it return array instead of stdclass.

这将使它返回数组而不是stdclass。

#2


1  

How do you initialize arr? You probably want to use an object instead of an Array. For example:

你如何初始化arr?您可能希望使用对象而不是Array。例如:

var arr = {};
arr[10] = 1;
arr[20] = 1;
arr[30] = 1;
arr[30] = 1;
var jsonified = JSON.stringify(arr);
console.log(jsonified);

#3


1  

Arrays in JavaScript work a bit differently than in PHP. You don't actually have an array with 4 elements, you have one with 41 elements right from the start. For example, when you say arr[ 10 ] = 1, every array element between 0 and 9 are automatically assigned undefined.

JavaScript中的数组与PHP中的数组略有不同。你实际上没有一个包含4个元素的数组,你从一开始就拥有41个元素。例如,当你说arr [10] = 1时,0到9之间的每个数组元素都会自动分配为undefined。

I suggest you switch the logic around: instead of using array keys to track values, use array values instead:

我建议你切换逻辑:而不是使用数组键来跟踪值,而是使用数组值:

arr = [ 10, 20, 30, 40 ];

Or, if you need other than binary values, use objects:

或者,如果您需要二进制值以外的其他值,请使用对象:

arr = [
    { score: 10, value: 1 },
    { score: 20, value: 5 }
];