This question already has an answer here:
这个问题在这里已有答案:
- How do I extract data from JSON with PHP? 2 answers
如何使用PHP从JSON中提取数据? 2个答案
I'm new into PHP and JSON and I have a problem, I want to retrieve a item and value from a 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)"
}
}
}
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.
我想从数据中检索项目 - >订单,例如“id”或“ip_address”。感谢您阅读本文,我希望有人可以帮助我,因为我迷路了,我最近开始编写代码而且我正在努力学习很多东西。
Regards!
2 个解决方案
#1
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.
test.json是您上传的json,将其放在名为test.json的文件中,并确保将其放在同一目录中。
<?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.
我的答案从文件中读取JSON,因为它直接在您的代码中转储,实际上它可能是,它会使代码更不易读,文件更麻烦。
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");
如果您不想将文件放在同一目录中,只需指定完整的文件路径即可。例如。的file_get_contents( “本/ DIR /位置/ test.json”);
You can read about how json_decode
works here, its essential we pass it the true
parameter to make our arrays associative.
你可以阅读json_decode如何在这里工作,它必不可少的我们传递真正的参数来使我们的数组关联。
#2
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.
您可以从JSON数据中提取您的need数组。您也可以使用循环来读取订单数组中的所有数据。
$array = json_decode($json, true);
$verbose = $array['data'];
$orderArray = $verbose['order'];
print_r($orderArray);
echo $orderArray['id'];
echo $orderArray['ip_address'];
#1
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.
test.json是您上传的json,将其放在名为test.json的文件中,并确保将其放在同一目录中。
<?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.
我的答案从文件中读取JSON,因为它直接在您的代码中转储,实际上它可能是,它会使代码更不易读,文件更麻烦。
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");
如果您不想将文件放在同一目录中,只需指定完整的文件路径即可。例如。的file_get_contents( “本/ DIR /位置/ test.json”);
You can read about how json_decode
works here, its essential we pass it the true
parameter to make our arrays associative.
你可以阅读json_decode如何在这里工作,它必不可少的我们传递真正的参数来使我们的数组关联。
#2
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.
您可以从JSON数据中提取您的need数组。您也可以使用循环来读取订单数组中的所有数据。
$array = json_decode($json, true);
$verbose = $array['data'];
$orderArray = $verbose['order'];
print_r($orderArray);
echo $orderArray['id'];
echo $orderArray['ip_address'];