简体   繁体   中英

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
                                )

                        )

                )

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM