簡體   English   中英

如何從php中的數組內的數組中獲取最大值

[英]How to get Maximum value from array inside array in php

我有這樣的數組,我想從數組類別中獲取分數標簽 我想得到這樣的結果,因為在色情類別中,它的分數是 0.25,非標准內容的分數是 0.1,所以結果應該是 label=>'pornography' 和分數 0.25。

我嘗試在像 $categories= array('pornography'=>0.25,'Non-Standard Content'=>0.1) 這樣的數組中獲取 label 和 score 的值

$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
                                )

                        )

                )

有人在這里發布了答案,但我不知道為什么它現在沒有出現。 我使用該解決方案來解決我的問題,這里是解決方案:

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

希望它會幫助某人。 感謝發布此解決方案的匿名者。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM