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 .
an object literal has no
.lengthyou can count properties using this method:
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:
then the json will be an array.. then you can use length
from this:
it becomes this using array_values(), note the indexes per item:
then encoded to json and stored to jsonString: