为了账号安全,请及时绑定邮箱和手机立即绑定

如何纠正 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 关注
  • 96 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信