Prateek Dewan
Prateek Dewan

Reputation: 1631

Best way to get list item with max(value) from a JSON object containing lists using PHP

This looks a bit convoluted, but it really isn't that bad. I have a JSON object that looks like this:

{'result': [{'label': {'field1': 'value1',
                       'field2': 'value2',
                       'field3': 'value3'},
             'names': {'field1': 'value4',
                       'field2': 'value5',
                       'field3': 'value6'},
             'score': {'field1': 0.9336825,
                       'field2': 0.9711186,
                       'field3': 0.7606185}},
            {'label': {'field1': 'value7',
                       'field2': 'value8',
                       'field3': 'value9'},
             'names': {'field1': 'value10',
                       'field2': 'value11',
                       'field3': 'value12'},
             'score': {'field1': 0.9336825,
                       'field2': 0.9711186,
                       'field3': 0.13707103}}]}

result is a list of dictionaries. Each such dictionary has 3 keys, label, name, and score. The values of each of these keys is again a dictionary with 3 fields, lets say field1, field2, and field3 for the sake of simplicity. There can be any number of lists in the result (usually less than 10 though). I want to get the list (and its subsequent items) which has the maximum ['score']['field3'] value. These values range between 0 and 1. What I've tried is a bit naive; iterate through the lists and keep a track of the max score:

$resp=json_decode($response,TRUE);
        $max = -9.99;
        foreach ($resp['result'] as $item)
        {
                if ($item['score']['field3'] > $max)
                {
                        $max = $item['score']['field3'];
                        $product_code = $item['label']['field1'];
                        $product_name = $item['names']['field1'];
                        $product_score = $item['score']['field1'];
                        $topic_code = $item['label']['field2'];
                        $topic_name = $item['names']['field2'];
                        $topic_score = $item['score']['field2'];
                        $intent_code = $item['label']['field3'];
                        $intent_name = $item['names']['field3'];
                        $intent_score = $item['score']['field3'];
                }
        }

Is there a better way to do this?

Upvotes: 0

Views: 51

Answers (1)

Adam Winter
Adam Winter

Reputation: 1934

There's not much room for improvement here IMO, but you might shave off a micro second this way:

$resp=json_decode($response,TRUE);
        $max = -9.99;
        $maxItem = [];
        foreach ($resp['result'] as $item)
        {
                if ($item['score']['field3'] > $max)
                {
                        $max = $item['score']['field3'];
                        $maxItem = $item;
                }
        }

$product_code = $maxItem['label']['field1'];
$product_name = $maxItem['names']['field1'];
$product_score = $maxItem['score']['field1'];
$topic_code = $maxItem['label']['field2'];
$topic_name = $maxItem['names']['field2'];
$topic_score = $maxItem['score']['field2'];
$intent_code = $maxItem['label']['field3'];
$intent_name = $maxItem['names']['field3'];
$intent_score = $maxItem['score']['field3'];

Either way, you can't find the max without iterating through all the values.

Upvotes: 1

Related Questions