How to get Maximum value from array inside array in php

I have my array like this, I want to fetch Score and Label from the array categories. I want to get my result like this as in here in category pornography its score is 0.25 and Non-Standard Content has score 0.1 so the result should be label=>'pornography' and score 0.25.

I tried getting values of label and score in an array like $categories= array('pornography'=>0.25,'Non-Standard Content'=>0.1)

$value = max($categories); then $value= 0.25
I want label also.

[text] => 
                    [taxonomy] => iab-qag
                    [language] => en
                    [categories] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [confident] => 
                                    [score] => 0.25
                                    [label] => Pornography
                                    [links] => Array
                                        (
                                            [0] => stdClass Object
                                                (
                                                    [rel] => self

                                                )

                                            [1] => stdClass Object
                                                (
                                                    [rel] => parent

                                                )

                                        )

                                    [id] => IAB25-3
                                )

                            [1] => stdClass Object
                                (
                                    [confident] => 1
                                    [score] => 0.1
                                    [label] => Non-Standard Content
                                    [links] => Array
                                        (
                                            [0] => stdClass Object
                                                (
                                                    [rel] => self

                                                )

                                        )

                                    [id] => IAB25
                                )

                        )

                )

1 answer

  • answered 2018-10-22 21:46 Ashutosh Roy

    Somebody posted here Answer but I don't know why it is not appearing now. I used that solution to solve my problem here is the solution:

    $maxScore = 0; $maxCat = [];
    foreach($data[$x]->categories as $c) {
            $cat[] = ['label' => $c->label, 'score' => $c->score];
                if ($maxScore < $c->score) {
                $maxScore = $c->score;
    
                $maxCat = [
                   'label' => $c->label, 'score' => $c->score
                   ];
               }
    

    Hope it will help somebody. Thanks to anonymous whoever posted this solution.