【发布时间】:2017-03-30 01:40:00
【问题描述】:
我正在尝试在 php 中创建一个数组,该数组将通过 ajax 调用在 javascript 中被解析为 json。我注意到用$empty = array() 实例化的数组的第一个索引返回为{"0":[{..values here..}], "success":true}。理想情况下,我会使用reply.data 和reply.success 访问它。回复。成功有效,但我似乎无法找到如何创建没有 0 作为第一个索引的数组。
我的php端代码:
$result = array();
$record = array();
$resp = mysqli_query($this->conn, $sql);
if(mysqli_num_rows($resp)>0){
while($row = mysqli_fetch_assoc($resp)){
$record = array();
foreach($row as $key => $val){
$record[$key] = $val;
}
array_push($result,$record);
unset($record);
}
//return json_encode($result, JSON_PRETTY_PRINT);
return $result;
当我在 javascript 中访问它时
success: function(data){
data = JSON.parse(data);
if(data.success==true){ //able to access success with "data.success"
//there is an empty
$scope.conn = "connecting to room";
console.log(data.data.room_id); //does not work because index is 0
console.log(JSON.stringify(data));
返回什么
{"0":[{"room_id":"20","host_id":"","resp_id":"","offer":"","answer":"","status":"0"}],"success":true}
【问题讨论】:
标签: php arrays json key associative-array