6

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 .

coolguy
  • 7,866
  • 9
  • 45
  • 71
  • `the result is 4` are you sure that it is related to your json? objects do not support length property. compare `alert({}.length);` and `alert([].length);` – Cheery Feb 16 '12 at 06:33
  • I think a [ is missing in my json but i dont know how to add this.. – coolguy Feb 16 '12 at 06:36

2 Answers2

8

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);
Joseph
  • 117,725
  • 30
  • 181
  • 234
1

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/

Cheery
  • 16,063
  • 42
  • 57
  • Is there something wrong with my json array am i missing a [ bracket ? – coolguy Feb 16 '12 at 06:45
  • @ubercooluk there is nothing wrong, when you are saving the associative array into the json format it becomes an object in order to preserve the keys. Compare `echo json_encode(array(1,2));` and `echo json_encode(array('a'=>1,'b'=>2));` – Cheery Feb 16 '12 at 06:48