admin管理员组

文章数量:1022496

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 es 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?

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 es 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?

Share Improve this question asked Jul 29, 2011 at 13:21 ThaiscorpionThaiscorpion 4791 gold badge8 silver badges19 bronze badges 2
  • 2 slots is not an array, it's another object. – Cᴏʀʏ Commented Jul 29, 2011 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 Commented Jul 29, 2011 at 13:26
Add a ment  | 

3 Answers 3

Reset to default 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 es from db
    $invSlots['slots'][$i] = $inventory_info3[$i];
}

As Zimzat said in a ment 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 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.

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++;

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)

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 es 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?

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 es 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?

Share Improve this question asked Jul 29, 2011 at 13:21 ThaiscorpionThaiscorpion 4791 gold badge8 silver badges19 bronze badges 2
  • 2 slots is not an array, it's another object. – Cᴏʀʏ Commented Jul 29, 2011 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 Commented Jul 29, 2011 at 13:26
Add a ment  | 

3 Answers 3

Reset to default 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 es from db
    $invSlots['slots'][$i] = $inventory_info3[$i];
}

As Zimzat said in a ment 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 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.

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++;

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)

本文标签: phpHow to get length of an array that is inside a json objectStack Overflow