JSON和PHP效率

时间:2015-09-24 20:44:09

标签: php json

如果我提供 id ,我会尝试找到最有效的方式来返回 productID 。例如:如果我提供 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"
                }
            ]
        },

....... 你明白了。它继续前进。 =)

2 个答案:

答案 0 :(得分:2)

嗯..缩小它有点你可以直接进入数据阵列并从那里开始。

$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

这是你在找什么?或者我误解了这个问题?

答案 1 :(得分:2)

假设您已将JSON解码为名为$array的关联数组,请构建一个ID映射到productID的数组:

$array = json_decode($url, true);
$results = array_column($array['results']['data'], 'productID', 'ID');
echo $results[17879]; //12550
{p <1}}需要

PHP&gt; = 5.5.0或使用Create view in sql server "names in each view or function must be unique"