2

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?

Thaiscorpion
  • 479
  • 1
  • 8
  • 18
  • 2
    `slots` is not an array, it's another object. – Cᴏʀʏ Jul 29 '11 at 13:23
  • 2
    Internally arrays begin at 0, but since you're forcing it to start at 1 it turns into an object. You'll either have to reset back to 0 for the array, or manually add a 'length' key to the array with the correct number. – Zimzat Jul 29 '11 at 13:26

3 Answers3

1

slots is not an array, it's another object. If it were being serialized as an array it would probably look more like:

{ "slots": 
    [
        { "0": "1" },
        { "1": "0" },
        { "2": "0" },
        ...
    ]
}

Or even just:

{ "slots": [ "1", "0", "0" ] }

Try changing your loop to:

for ($i = 0; $i < $player_max_slots; $i++) { //player max slots comes from db
    $invSlots['slots'][$i] = $inventory_info3[$i];
}

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.

<?php
echo json_encode(array("test","test","test"));
echo json_encode(array(0=>"test",3=>"test",7=>"test"));
?>

Will give :

["test","test","test"]
{"0":"test","3":"test","7":"test"}

Arrays are only returned if you don't define an index, or if your indexes are sequential and start at 0.

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
1

You've declared an associative array, not an indexed array. So you can't use length.

To get the count from an associative array, you need to iterate through its keys:

var count=0;
for (var key in $invSlots['slots'])
    count++;
Tim Rogers
  • 21,297
  • 6
  • 52
  • 68
0

You can make a little function:

function len(obj) {
    var ret = 0;
    for (var i in obj) ret++;
    return ret;
}

var maxSlots = len(myResponse.slots)
qwertymk
  • 34,200
  • 28
  • 121
  • 184
  • 1
    you'll also need to check `if (obj.hasOwnProperty(i))` to prevent this function from picking up methods attached to an object. – zzzzBov Jul 29 '11 at 13:28
  • hardly the point. If you create a generic function to perform some operation, you ought to make sure it *works*. In this case, you wont be having any functions on the object as it's sent from JSON, but it's quite possible that the functions could be added after the object was received. – zzzzBov Jul 29 '11 at 13:38
  • Also if someone modifies the object prototype, like the Prototype library (which could get included at some point down the road). Plus, knowing the 'gotchas' is one very good way to learn the limitations and 'magic' behind a language, preferably without banging your head against them first. – Zimzat Jul 29 '11 at 14:22