Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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_encode($array);

The resulted jsonString is something like this

{"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); the result is 4 But i want the result to be 2 am i doing something wrong .

like image 948
coolguy Avatar asked Feb 16 '12 06:02

coolguy


2 Answers

an object literal has no .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.

to turn them to numeric indices, all you have to do is use array_values before encoding them to json:

json_encode(array_values($array));

then the json will be an array.. then you can use length

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(
[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:

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);
like image 166
Joseph Avatar answered Oct 10 '22 21:10

Joseph


Objects do not support length property: alert({}.length); gives undefined and alert([].length); gives 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/

like image 44
Cheery Avatar answered Oct 10 '22 22:10

Cheery