首页 > 解决方案 > 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”。感谢您阅读本文,我希望有人可以帮助我,因为我迷路了,我最近开始编写代码并且我正在尝试学习很多东西。

问候!

标签: phpjson

解决方案


您可以从 JSON 数据中提取您需要的数组。您也可以使用循环来读取订单数组中的所有数据。

$array = json_decode($json, true);

$verbose = $array['data'];
$orderArray = $verbose['order'];

print_r($orderArray);

echo $orderArray['id'];
echo $orderArray['ip_address'];

推荐阅读