我试图找到最有效的方法来返回productID如果我提供一个id。例如:如果我提供id 17879,我如何让php返回productID 12550
我所拥有的在下面,但我想一定有办法做到这一点。
提前感谢您的任何建议!
$sub_type = 17879; #Could be any id
$pid = 0;
$json = file_get_contents($url);
$obj = json_decode($json, true);
foreach ($obj as $val) {
if (is_array($val)) {
foreach ($val as $val) {
if (is_array($val)) {
foreach ($val as $val) {
if ($val['id'] == $sub_type)
$pid = $val['productID'];
}
}
}
}
}
echo $pid;
$url提供以下JSON
{
"meta": {
"time": "Thursday, September 24, 2015 7:43:53 PM",
"statusCode": 200
},
"results": {
"errors": [],
"messages": [],
"data": [
{
"id": 17879,
"productID": 12550,
"name": " Bill of Rights 8.5x11 ",
"description": "",
"hasTemplate": true,
"deliveredPrices": [
{
"description": "FedEx Ground",
"price": 84.4,
"country": null,
"countryCode": null,
"created": "2015-02-25T16:14:49.283"
},
{
"description": "FedEx 3 Day",
"price": 164.4,
"country": null,
"countryCode": null,
"created": "2015-02-25T16:14:49.283"
},
{
"description": "FedEx 2 Day",
"price": 224.4,
"country": null,
"countryCode": null,
"created": "2015-02-25T16:14:49.287"
},
{
"description": "FedEx Overnight PM",
"price": 304.4,
"country": null,
"countryCode": null,
"created": "2015-02-25T16:14:49.287"
}
]
},
{
"id": 17880,
"productID": 12558,
"name": "Annual Client Survey 8.5x11 (5 pages)",
"description": "",
"hasTemplate": true,
"deliveredPrices": [
{
"description": "FedEx Ground",
"price": 84.4,
"country": null,
"countryCode": null,
"created": "2015-02-26T13:34:01.933"
},
{
"description": "FedEx 3 Day",
"price": 164.4,
"country": null,
"countryCode": null,
"created": "2015-02-26T13:34:01.933"
},
{
"description": "FedEx 2 Day",
"price": 224.4,
"country": null,
"countryCode": null,
"created": "2015-02-26T13:34:01.937"
},
{
"description": "FedEx Overnight PM",
"price": 304.4,
"country": null,
"countryCode": null,
"created": "2015-02-26T13:34:01.937"
}
]
},
{
"id": 17881,
"productID": 12559,
"name": "Estate Planning 8.5x11 ",
"description": "",
"hasTemplate": true,
"deliveredPrices": [
{
"description": "FedEx Ground",
"price": 84.4,
"country": null,
"countryCode": null,
"created": "2015-02-26T14:19:09.29"
},
{
"description": "FedEx 3 Day",
"price": 164.4,
"country": null,
"countryCode": null,
"created": "2015-02-26T14:19:09.297"
},
{
"description": "FedEx 2 Day",
"price": 224.4,
"country": null,
"countryCode": null,
"created": "2015-02-26T14:19:09.307"
},
{
"description": "FedEx Overnight PM",
"price": 304.4,
"country": null,
"countryCode": null,
"created": "2015-02-26T14:19:09.317"
}
]
},
……你懂的。它还在继续。div =)
嗯…缩小一点范围你可以直接跳到数据数组然后从那里开始。
$obj = json_decode($json, true);
$sub_type = 17879;
$pid = 0;
foreach($obj['results']['data'] as $data){
if($data['id'] == $sub_type){
$pid = $data['productID'];
}
}
echo $pid; //12550
这是你要找的吗?还是我误解了你的问题?
假设您已经将JSON解码为一个名为$array
的关联数组,构建一个将ID
映射到productID
的数组:
$array = json_decode($url, true);
$results = array_column($array['results']['data'], 'productID', 'ID');
echo $results[17879]; //12550
PHP>= 5.5.0需要array_column()
或使用PHP实现的array_column()