I create a json object in php and send it back to the main page:
$invSlots = array();
$invSlots['slots'] = array();
for( $i = 1; $i < $player_max_slots+1; $i++){ //player max slots comes from db
$invSlots['slots'][$i] = $inventory_info3[$i];
}
$json = $invSlots;
$encoded = json_encode($json);
die ($encoded);
And the post response is this:
{"slots": {
"1": "1",
"2": "0",
"3": "0",
"4": "4",
"5": "0",
"6": "0",
"7": "3",
"8": "0",
"9": "0",
"10": "0",
"11": "2",
"12": "0"
}
}
Im trying to get the amount of slots like so:
var myResponse = JSON.decode('(' + responseText + ')'); //decode server json response
maxSlots = myResponse.slots.length; //set max slots
but myResponse.slots.length just returns undefined, how can i fix it?
slotsis not an array, it’s another object. If it were being serialized as an array it would probably look more like:Or even just:
Try changing your loop to:
As Zimzat said in a comment above, once your array’s indices start at 0 you should get an array of
slots when you serialize your object to JSON.Actually, according to some guy at the php.net forums, you need to be aware of index arrays.
Will give :
Arrays are only returned if you don’t define an index, or if your indexes are sequential and start at 0.