【发布时间】:2017-10-02 15:33:07
【问题描述】:
我正在从 web 服务接收下面的 json 有效负载,我试图在解码 whlist 后使用循环将其分成 3 个不同的数组,保持相同的数组键:
{
"id": 5705376,
"title": "Satellite Installation",
"created_at": "2017-09-07T14:02:19.000Z",
"updated_at": "2017-09-07T14:02:19.000Z",
"customer_id": 3126803,
"way_points": [{
"id": 7405587,
"lat": -26.0578251,
"lng": 28.02189520000002,
"task_id": 5705376,
"done": false,
"allow_editing_inventory": true,
"customer_contact_ids": [3126803],
"customer": {
"original_phone_number": null,
"last_open_at": null,
"last_order_at": null,
"uploaded_profile_image": {
"url": "/images/avatar.png"
},
"original_lat": null,
"original_lng": null,
"district": null,
"house_number": null,
"street": null
},
"full_address": "8 Anslow Ln, Bryanston, Sandton, 2191, South Africa"
}],
"customer": {
"id": 3126803,
"name": "Lance",
"address": "8 Anslow Ln, Bryanston, Sandton, 2191, South Africa",
"lat": -26.0578251,
"lng": 28.0218952,
"created_at": "2017-08-29T10:00:32.360Z",
"updated_at": "2017-09-07T14:02:19.860Z",
"phone": null,
"merchant_id": 11221,
"allow_login": false,
"stripe_id": null,
"original_phone_number": null,
"last_open_at": null,
"last_order_at": null,
"uploaded_profile_image": {
"url": "/images/avatar.png"
},
"original_lat": null,
"original_lng": null,
"house_number": null,
"street": null
},
"late": false,
"external_id": "5705376",
"uuid": "28095e33-b30c-4e35-98d2-aae6ad428f66",
"dispatcher_id": null,
"team_ids": [12422],
"ready_to_execute": true,
"tip_driver_enabled": false,
"automatically_assigned": false,
"task_inventories": null,
"task_notes": null
}
对于“way_points”键中的值,我想在解码后提出一个称为航点的数组,同时忽略其中的客户数组。我也想在这个过程中把“id”键名改成“waypoint_id”。
对于“customers”键中的值(在主数组中),我想在解码后提出一个名为“waypoint”的数组,同时分配“url”的值 键到“上传的个人资料图像”键。我也想在这个过程中把“id”键名改成“customer_id”。
上述数组之外的任何元素(因此主数组中未分配给数组的元素),我希望它们形成一个称为“订单”的数组。
我该怎么做。我是 LOOPS 的新手
【问题讨论】:
-
为什么需要不同的变量?只需使用
json_decode()解码 json 即可获得所需的一切。 -
您分享的 JSON 格式无效,您确定那是正确的吗?
-
我对无效的 json 感到抱歉,我编辑了我的答案
标签: php arrays json multidimensional-array