如何纠正 php 中重复的 json 数组

我有以下代码从 mysql 数据库读取数据。不幸的是我似乎找不到删除这个的方法"status": {


这是代码片段


// required headers

header("Access-Control-Allow-Origin: *");

header("Content-Type: application/json; charset=UTF-8");

  

// database connection will be here


// include database and object files

include_once '../config/database.php';

include_once '../objects/status.php';

  

// instantiate database and status object

$database = new Database();

$db = $database->getConnection();

  

// initialize object

$status = new Status($db);

  

// query status

$stmt = $status->read();

$num = $stmt->rowCount();

  

// check if more than 0 record found

if($num>0){

  

    // status array

    $status_arr=array();

    $status_arr["status"]=array();

  

    // retrieve our table contents

    // fetch() is faster than fetchAll()

    // http://stackoverflow.com/questions/2770630/pdofetchall-vs-pdofetch-in-a-loop

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){

        // extract row

        // this will make $row['name'] to

        // just $name only

        extract($row);

  

        $status_item = array(

            "id" => $status_id,

            "name" => html_entity_decode($status_name),

            "created" => $status_created,

            "modified" => $status_modified,

            "state" => $status_state

        );


        array_push($status_arr["status"], $status_item);

    }

  

    // set response code - 200 OK

    http_response_code(200);

  

    // show status data in json format


    echo json_encode(

        array("response_code" => 0, "response_message" => "Request processed successfully", "status" => $status_arr)

    );

}

else{

  

    // set response code - 404 Not found

    http_response_code(404);

  

    // tell the user no status found

    echo json_encode(

        array("response_code" => 1, "response_message" => "No status found.")

    );



请注意,在所需的输出中不应有 "status": {},而应仅具有 "status": []。我应该对上面提供的代码进行哪些修改?


精慕HU
浏览 68回答 1
1回答

MMTTMM

echo json_encode(  array(    "response_code" => 0,    "response_message" => "Request processed successfully",    "status" => $status_arr["status"]  ));
打开App,查看更多内容
随时随地看视频慕课网APP