php - PHP - 检索 JSON 数据和值
问题描述
我是 PHP 和 JSON 的新手,我有一个问题,我想从 JSON 中检索一个项目和值:
{
"status": true,
"webhook_type": 100,
"data": {
"product": {
"id": "lSEADIQ",
"attachment_id": null,
"title": "Registration",
"description": null,
"image": null,
"unlisted": false,
"type": "service",
"price": 1,
"currency": "EUR",
"email": {
"enabled": false
},
"stock_warning": 0,
"quantity": {
"min": 1,
"max": 1
},
"confirmations": 1,
"custom_fields": [
{
"name": "Forum username",
"type": "text",
"required": true
}
],
"gateways": [
"Bitcoin"
],
"webhook_urls": [],
"dynamic_url": "",
"position": null,
"created_at": "2018-10-01 12:51:12",
"updated_at": "2018-10-01 12:55:46",
"stock": 9223372036854776000,
"accounts": []
},
"order": {
"id": "8e23b496-121a-4dc6-8ec4-c45835680db2",
"created_at": "Tue, 02 Oct 2018 00:54:56 +0200",
"paid_at": null,
"transaction_id": null,
"confirmations": 1,
"required_confirmations": 3,
"received_amount": 0,
"crypto_address": "1NeNQws7JLbTr6bjekfeaXSV7XiyRsv7V8",
"crypto_amount": "0.4815",
"quantity": 1,
"price": 19.99,
"currency": "EUR",
"exchange_rate": "1.21",
"gateway": "BTC",
"email": "webhook@site.gg",
"ip_address": "123.456.789.111",
"agent": {
"geo": {
"ip": "214.44.18.6",
"iso_code": "US",
"country": "United States"
},
"data": {
"is_mobile": false,
"is_table": false,
"is_desktop": true,
"browser": {
"name": "Chrome",
"version": "63.0.3239.132"
}
}
},
"custom_fields": [
{
"name": "user_id",
"value": 184191
}
],
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3)"
}
}
}
我想从数据 -> 订单中检索项目,例如“id”或“ip_address”。感谢您阅读本文,我希望有人可以帮助我,因为我迷路了,我最近开始编写代码并且我正在尝试学习很多东西。
问候!
解决方案
您可以从 JSON 数据中提取您需要的数组。您也可以使用循环来读取订单数组中的所有数据。
$array = json_decode($json, true);
$verbose = $array['data'];
$orderArray = $verbose['order'];
print_r($orderArray);
echo $orderArray['id'];
echo $orderArray['ip_address'];
推荐阅读
- python - 在 pycharm 中配置 Pipenv 环境
- html - Div 边距始终位于页面右侧
- python - 如何修复预写文件中的“FileNotFoundError”?
- asp.net-mvc - 如何在视图中使用 if 语句 (@doc.value == "N/A" )
- xml - 使用 XSLT 将 XML 文件拆分为多个文件
- git - 2+ 用户如何从 sudo 帐户使用 git
- scala - NoSuchMethodError: java.lang.String.lines()Ljava/util/stream/Stream; 尝试使用 ScalaMock 运行测试时
- c# - WebApplicationFactory 在另一个项目目录中使用 Startup.Cs
- html - 更改按钮高度时如何使文本始终保持水平居中
- c# - 跟踪静态字典中的任务并等待在单独的请求线程中启动的任务是安全的吗?