亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何糾正 php 中重復的 json 數組

如何糾正 php 中重復的 json 數組

PHP
精慕HU 2023-11-03 15:28:27
我有以下代碼從 mysql 數據庫讀取數據。不幸的是我似乎找不到刪除這個的方法"status": {這是代碼片段// required headersheader("Access-Control-Allow-Origin: *");header("Content-Type: application/json; charset=UTF-8");  // database connection will be here// include database and object filesinclude_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 foundif($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": []。我應該對上面提供的代碼進行哪些修改?
查看完整描述

1 回答

?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

echo json_encode(

  array(

    "response_code" => 0,

    "response_message" => "Request processed successfully",

    "status" => $status_arr["status"]

  )

);


查看完整回答
反對 回復 2023-11-03
  • 1 回答
  • 0 關注
  • 143 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號