私はPHPでJSONオブジェクトを作成し、メインページに戻ってそれを送信します。jsonオブジェクトの内部にある配列の長さを取得する方法は?
$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);
をし、POST応答はこれです:そうのようなスロットの量を取得しようとし
{"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"
}
}
イム:
var myResponse = JSON.decode('(' + responseText + ')'); //decode server json response
maxSlots = myResponse.slots.length; //set max slots
しかし、myResponse.slots.lengthは単に未定義を返します。どうすれば修正できますか?
'slots'が配列ではない、それは別のオブジェクトです。 –
内部的な配列は0から始まりますが、1から始めるように強制しているので、オブジェクトに変わります。あなたは配列の0に戻ってリセットするか、配列に適切な数の「長さ」キーを手動で追加する必要があります。 – Zimzat