如何从PHP数组中的数组中获取最大值

时间:2018-10-22 19:39:23

标签: php arrays

我有这样的数组,我想从数组类别中获取得分标签。我想要得到这样的结果,例如在色情类别中,它的得分是0.25,而非标准内容的得分是0.1 因此结果应为label =>'pornography',得分为0.25。

我试图在数组中获取label和score的值 $ categories = array('pornography'=> 0.25,'Non-Standard Content'=> 0.1)

$ value = max($ categories); 然后$ value = 0.25
我也要标签。

[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 个答案:

答案 0 :(得分:0)

有人在这里发布答案,但我不知道为什么现在没有出现。我使用该解决方案来解决我的问题,这里是解决方案:

$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
               ];
           }

希望它将对某人有帮助。感谢匿名张贴此解决方案的人。