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
3 Answers
Reset to default 1slots
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 slot
s 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
3 Answers
Reset to default 1slots
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 slot
s 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
版权声明:本文标题:php - How to get length of an array that is inside a json object? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745563959a2156335.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论