Php 创建具有键值的空数组

Php 创建具有键值的空数组,php,arrays,json,key,associative-array,Php,Arrays,Json,Key,Associative Array,我正在尝试用php创建一个数组,该数组将通过ajax调用在javascript中解析为json。我注意到用$empty=array()实例化的数组的第一个索引返回为{“0”:[{..values here..}],“success”:true}。理想情况下,我会使用reply.data和reply.success访问它。答案是肯定的。成功是可行的,但我似乎找不到如何在没有0作为第一个索引的情况下创建数组 我的php端代码: $result = array(); $record =

我正在尝试用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}
用这个,

$result = array();
$resp = mysqli_query($this->conn, $sql);
if(mysqli_num_rows($resp)>0){
    while($row = mysqli_fetch_assoc($resp)){
        foreach($row as $key => $val){
            $result["data"][$key] = $val
        }
    }
}
//return json_encode($result, JSON_PRETTY_PRINT);
return $result;
用这个,

$result = array();
$resp = mysqli_query($this->conn, $sql);
if(mysqli_num_rows($resp)>0){
    while($row = mysqli_fetch_assoc($resp)){
        foreach($row as $key => $val){
            $result["data"][$key] = $val
        }
    }
}
//return json_encode($result, JSON_PRETTY_PRINT);
return $result;

我认为你把过程复杂化了

foreach循环似乎没有必要,因为您可以将$row加载到数组中

public function xxx()
{
    $result = array();
    $resp = mysqli_query($this->conn, $sql);
    if(mysqli_num_rows($resp)>0) {
        $result['success'] = 'true';

        while($row = mysqli_fetch_assoc($resp)){
            $result['data'][] = $row;
        }

    } else {
        $result['success'] = 'false';
    }
    return $result;
}

// instantiate object 
//$obj = new Whatever the class is called()
echo json_encode($obj->xxx());

我认为你把过程复杂化了

foreach循环似乎没有必要,因为您可以将$row加载到数组中

public function xxx()
{
    $result = array();
    $resp = mysqli_query($this->conn, $sql);
    if(mysqli_num_rows($resp)>0) {
        $result['success'] = 'true';

        while($row = mysqli_fetch_assoc($resp)){
            $result['data'][] = $row;
        }

    } else {
        $result['success'] = 'false';
    }
    return $result;
}

// instantiate object 
//$obj = new Whatever the class is called()
echo json_encode($obj->xxx());

这回答了我的问题,但我应该补充一点,我通过一个reply函数运行我的响应,所以现在它返回这个
{“reply”:[{“data”:{“room_id”:“20”,…}],“success”:true}
为什么它似乎返回一个数组?php reply func:
public function reply($success,$data){$reply=array();if(sizeof($data)!=0){foreach($key=>data){$reply[$key]=$val;}}返回json_encode($reply);}
具有数字键的php数组,以
0
开头,且数字(即1、3、5)中没有任何间隙,将转换为json_encode中的数组。这是非常标准的,因为它们在JS中可以使用相同的方法。您还可以将
JSON\u FORCE\u对象
常量传递给JSON\u encode。这回答了我的问题,但我应该补充一下,我通过一个reply函数运行我的响应,所以现在它返回这个
{“reply”:[{“data”:{“room\u id”:“20”,…}}],“success”:true}
为什么它似乎返回一个数组?php reply func:
public function reply($success,$data){$reply=array();if(sizeof($data)!=0){foreach($key=>data){$reply[$key]=$val;}}返回json_encode($reply);}
具有数字键的php数组,以
0
开头,且数字(即1、3、5)中没有任何间隙,将转换为json_encode中的数组。这是非常标准的,因为它们在JS中可以使用相同的方法。您还可以将
JSON\u FORCE\u对象
常量传递给JSON\u encode。