1

I'm trying to find the most efficient way to return the productID if I supply an id. For example: If I supply the id 17879, how can I get php to return the productID 12550

What I have is below, but I figure there has to be a way to do it.

Thanks in advance for any suggestions!


    $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 supplies the following 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"
                }
            ]
        },

....... You get the idea. It keeps going. =)

2 Answers 2

2

Well.. narrowing it down a bit you could sort of jump right into the data array and go from there.

$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

Is this what you are looking for? Or did I misunderstand the question?

Sign up to request clarification or add additional context in comments.

1 Comment

That is exactly what I was looking for! Thanks a billion.
2

Assuming that you have decoded the JSON into an associative array called $array, build an array with ID mapped to productID:

$array = json_decode($url, true);
$results = array_column($array['results']['data'], 'productID', 'ID');
echo $results[17879]; //12550

PHP >= 5.5.0 needed for array_column() or use the PHP Implementation of array_column()

1 Comment

Nice! In this particular case PHP 5.4 is still in use. Although, thanks for the awesome future reference!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.