0

I am trying to create an array in php that will be parsed as json in javascript via ajax call. I noticed that the first index of an array instantiated with $empty = array() returns as {"0":[{..values here..}], "success":true}. Ideally I would access it using reply.data and reply.success. The reply. success works, but I can't seem to find how to create an array without a 0 as the first index.

My php side code:

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

When I access it within 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));

What it returns

{"0":[{"room_id":"20","host_id":"","resp_id":"","offer":"","answer":"","status":"0"}],"success":true}
0

2 Answers 2

1

use this,

$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;
Sign up to request clarification or add additional context in comments.

2 Comments

This answered my question, but I should have added that I run my responses through a reply function so now it's returning this {"reply":[{"data":{"room_id":"20",...}}],"success":true} why does it seem to be returning an array? php reply func: public function reply($success,$data){ $reply = array(); if(sizeof($data)!=0){ foreach($data as $key => $val){ $reply['reply'][$key] = $val; } } return json_encode($reply); }
A php array that has numeric keys, starts with 0 and doesn't have any gaps in the numbers (i.e. 1, 3, 5) will be converted to an array in json_encode. This is pretty standard as they can both be used the same in JS. You can also pass the JSON_FORCE_OBJECT constant to json_encode.
1

I think you are rather over complicating the process

The foreach loop appears to be unnecessary as you can just load $row into the array.

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());

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.