简体   繁体   English

json和多维数组

[英]json and multidimensional array

I have a multidimensional array like this 我有一个像这样的多维数组

Array
(
    [1] => Array
        (
            [product_id] => 1
            [product_model] => HFJ5G1.5
            [product_type] => plat
            [product_return] => graviteits

        )

    [2] => Array
        (
            [product_id] => 2
            [product_model] => HHJ5S2.5
            [product_type] => holle plunjer
            [product_return] => veer       

        )
    );  //Only 2 are shown here i have around 110 values

And i encoded this to json by 我将此编码为json

json_encode($array);

The resulted jsonString is something like this 结果jsonString是这样的

{"1":{"product_id":"1","product_model":"HFJ5G1.5","product_type":"plat","product_return":"graviteits"},"2":{"product_id":"2","product_model":"HHJ5S2.5","product_type":"holle plunjer","product_return":"veer"}}

when i do alert(jsonString.length); 当我做警报(jsonString.length); the result is 4 But i want the result to be 2 am i doing something wrong . 结果是4但我希望结果是凌晨2点我做错了什么。

an object literal has no .length 对象文字没有.length

you can count properties using this method : 您可以使用此方法计算属性:

var count = 0;

for (i in jsonString) {
    if (jsonString.hasOwnProperty(i)) {
        count++;
    }
}

alert(count); //count shall have length for you

OR 要么

since your array didn't have numeric indices (starting from 0), it assumed you used an associative array , hence they dumped an object of items rather than an array of items . 由于你的数组没有数字索引(从0开始),它假设你使用了一个关联数组 ,因此它们转储了一个项目对象而不是一个项目数组

to turn them to numeric indices, all you have to do is use array_values before encoding them to json: 要将它们转换为数字索引,您所要做的就是在将它们编码为json之前使用array_values

json_encode(array_values($array));

then the json will be an array.. then you can use length 然后json将是一个数组.. 然后你可以使用长度

from this: 由此:

Array(
[1] => Array(
        [product_id] => 1
        [product_model] => HFJ5G1.5
        [product_type] => plat
        [product_return] => graviteits
    )
[2] => Array(
        [product_id] => 2
        [product_model] => HHJ5S2.5
        [product_type] => holle plunjer
        [product_return] => veer       
    )
);

it becomes this using array_values(), note the indexes per item: 使用array_values()成为这个,记下每个项目的索引:

Array(
[0] => Array(
        [product_id] => 1
        [product_model] => HFJ5G1.5
        [product_type] => plat
        [product_return] => graviteits
    )
[1] => Array(
        [product_id] => 2
        [product_model] => HHJ5S2.5
        [product_type] => holle plunjer
        [product_return] => veer       
    )
);

then encoded to json and stored to jsonString: 然后编码为json并存储到jsonString:

jsonString = [
    {
        "product_id": "1",
        "product_model": "HFJ5G1.5",
        "product_type": "plat",
        "product_return": "graviteits"
    },
    {
        "product_id": "2",
        "product_model": "HHJ5S2.5",
        "product_type": "holle plunjer",
        "product_return": "veer"
    }
];

alert(jsonString.length);

Objects do not support length property: alert({}.length); 对象不支持length属性: alert({}.length); gives undefined and alert([].length); 给出undefinedalert([].length); gives 0 . 给出0 To find the 'length' of the top level you could do it like: 要找到顶级的“长度”,您可以这样做:

var arr ={"1":{"product_id":"1","product_model":"HFJ5G1.5",
                 "product_type":"plat","product_return":"graviteits"},
          "2":{"product_id":"2","product_model":"HHJ5S2.5",
                "product_type":"holle plunjer","product_return":"veer"}};
var len = 0;
for(var i in arr) len++;
alert(len);

http://jsfiddle.net/uszaV/ http://jsfiddle.net/uszaV/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM