Home  >  Article  >  Backend Development  >  求多维数组里的最大值

求多维数组里的最大值

WBOY
WBOYOriginal
2016-06-23 13:30:391792browse

数组是这样的:
Array
(
    [0] => Array
        (
            [CampaignID] => 2304111
            [ExistStatus] => 2
            [CampaignName] => 7月促销中心
            [CampaignType] => 定向推广计划
            [AvgCommission] => 1.71 %
            [Exist] => 1
            [ShopKeeperID] => 18791530
            [Properties] => 否
        )

    [1] => Array
        (
            [CampaignID] => 2979322
            [CampaignName] => 新年 促销价
            [CampaignType] => 定向推广计划
            [AvgCommission] => 4.90 %
            [Exist] => 
            [ShopKeeperID] => 18791530
            [Properties] => 否
        )

    [2] => Array
        (
            [CampaignID] => 7275933
            [CampaignName] => 感恩
            [CampaignType] => 定向推广计划
            [AvgCommission] => 1.53 %
            [Exist] => 
            [ShopKeeperID] => 18791530
            [Properties] => 否
        )

)

找出AvgCommission为最大值的,同时还要带上CampaignID
 
让结果显示为array(2979322,4.9);

我算法太差了,找了好多都看不明白!!!


回复讨论(解决方案)

foreach($arr as $v) $r[] = $v['AvgCommission'];array_multisort($r, SORT_DESC, SORT_NUMERIC, $arr);$r = array($arr[0]['CampaignID'], $arr[0]['AvgCommission'] + 0);print_r($r);

你就是额的神

那也未必,这样的代码组合是定式,多接触就熟练了

如果是数据库查询,那么
SELECT CampaignID, AvgCommission FROM tbl_name ORDER BY AvgCommission DESC
你一定不陌生

其实他们是一一对应的

谢谢神,感觉明白了些了!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:基础问题求普及,谢谢Next article:php调用C程序