0

I'm new into PHP and JSON and I have a problem, I want to retrieve a item and value from a 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)"
    }
  }
}

I want to retrieve items from data -> order, for example "id" or "ip_address". Thank you for read this, I hope someone can help me in this, because I'm lost, I started to code very recently and I'm trying to learn a lot.

Regards!

  • 1
    your need a function call json_decode. Read about it here [link](http://php.net/manual/en/function.json-decode.php). Use the TRUE option to make an associative array. Try this and post some code if not working. – bcperth Oct 01 '18 at 23:16

2 Answers2

0

You can extract your need array from JSON data. You can use a loop too to read all your data inside the order array.

$array = json_decode($json, true);

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

print_r($orderArray);

echo $orderArray['id'];
echo $orderArray['ip_address'];
Muhammad Usman
  • 1,403
  • 13
  • 24
0

Where test.json is the json you uploaded, place it in a file named test.json and ensure its placed in the same directory.

<?php

$load = file_get_contents("test.json") or die("JSON load failed");    
$json_a = json_decode($load, true);
print $json_a['data']['order']['ip_address'] . "\n"; 

?>

Gives:

123.456.789.111

My answer reads the JSON from a file as were it dumped directly in your code, which indeed it could be, it would make the code less readable and your file more messy.

If you dont want to place the file in the same directory, simply specify the full file path. E.g. file_get_contents("this/dir/here/test.json");

You can read about how json_decode works here, its essential we pass it the true parameter to make our arrays associative.

omeanwell
  • 1,847
  • 1
  • 10
  • 16