简体   繁体   English

用php格式化json中的返回数据

[英]formatting returned data in json with php

i'm pulling data from a web API which returns the data like this我正在从一个 web API 中提取数据,它返回这样的数据

[
    {
        "ID": 1,
        "Name": "Puru Farms",
        "Farm_Size": "24 acres",
        "Town": "Dansoman",
        "Farm_Owner_ID": 6,
        "Route_ID": 1,
        "Region_ID": 1,
        "District_ID": 9
    }
]

but i want the data to be returned in this format但我希望数据以这种格式返回

{"data":
[
    {
        "ID": 1,
        "Name": "Puru Farms",
        "Farm_Size": "24 acres",
        "Town": "Dansoman",
        "Farm_Owner_ID": 6,
        "Route_ID": 1,
        "Region_ID": 1,
        "District_ID": 9
    }
]}

From the API i return the records with this echo json_encode($data);我从 API 返回带有此echo json_encode($data);的记录echo json_encode($data);

try this尝试这个

replace your更换你的

json_encode($data)

with

$newdata["data"] = $data;
json_encode($newdata);

像这样返回您的 json 数据:-

   json_encode(['data' => $data]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM