【问题标题】:Get the maximum value and the key of the corresponding array from an array of arrays in PHP从PHP中的数组数组中获取对应数组的最大值和key
【发布时间】:2021-12-16 16:39:09
【问题描述】:

对于这个令人困惑的问题,我真的很抱歉,但基本上我有一个由多个其他数组组成的 PHP 数组。在这些数组中,我想在相关数组的 cpmbid 列中提取最大值的数组。

Array ( [0] => Array ( [cid] => 1 [accountid] => anaccount [orderid] => two [campaigntype] => CPM [bid] => 1.40000 [creativename] => Creative 2 [imgsource] => https://i1.wp.com/test-bucket-dfn.s3.us-west-2.amazonaws.com/wp-content/uploads/2019/07/24122252/NATPE-300x250.jpg?ssl=1 [clickthrough] => https://google.com/search?q=ad3 [targethostname] => localhost [targetpath] => all [keyvalue1] => my [keyvalue2] => any [keyvalue3] => any [width] => 300 [height] => 250 [dayrem] => 4 [clickrem] => 0 [imprem] => 1019 ) [1] => Array ( [cid] => 2 [accountid] => anaccount [orderid] => one [campaigntype] => CPM [bid] => 1.60000 [creativename] => Creative 2 [imgsource] => https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSm2kj7NQ0uuIxYqSA4-3dd1-I_x2xcpq2LmMfAcUOTG2AVv2n6bCkONgr1uVNJJO8EFIg&usqp=CAU [clickthrough] => https://google.com/search?q=ad2 [targethostname] => localhost [targetpath] => /home/no [keyvalue1] => hello [keyvalue2] => my [keyvalue3] => friend [width] => 300 [height] => 250 [dayrem] => 2 [clickrem] => 0 [imprem] => 753 ) [2] => Array ( [cid] => 3 [accountid] => anaccount [orderid] => three [campaigntype] => CPM [bid] => 2.30000 [creativename] => Creative 3 [imgsource] => https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRmZIh09uRp3ADOszQz6APBuCOmd6H_cQ-Dew&usqp=CAU [clickthrough] => https://google.com/search?q=ad4 [targethostname] => localhost [targetpath] => /fruitsalad/page.html [keyvalue1] => any [keyvalue2] => any [keyvalue3] => any [width] => 300 [height] => 250 [dayrem] => 2 [clickrem] => 0 [imprem] => 1000 ) [3] => Array ( [cid] => 4 [accountid] => anaccount [orderid] => two [campaigntype] => CPM [bid] => 1.90000 [creativename] => Creative 4 [imgsource] => https://i1.wp.com/test-bucket-dfn.s3.us-west-2.amazonaws.com/wp-content/uploads/2019/07/24122252/NATPE-300x250.jpg?ssl=1 [clickthrough] => https://google.com/search?q=ad5 [targethostname] => localhost [targetpath] => all [keyvalue1] => any [keyvalue2] => any [keyvalue3] => any [width] => 300 [height] => 250 [dayrem] => 4 [clickrem] => 0 [imprem] => 4013 ) )
//This is the Array, I want to select the array in which the highest 'bid' value is, any answers?

如果这是一个重复的问题,我很抱歉,我很感激任何答案。 谢谢!

【问题讨论】:

    标签: php arrays multidimensional-array


    【解决方案1】:

    检查以下代码,它会根据值找到最大值然后是最大数组:

    $maxval=max(array_column($array,"bid")); 
    $maxarray=array_keys(array_column($array,"bid"),$maxval);
    print_R($array[$maxarray[0]]); 
    

    $array = 你的数组,

    【讨论】:

    • @Timothy 我已经更新了代码,所以如果您有重复的最高值,那么您将获得 $maxarray 中的所有键。
    【解决方案2】:

    您可以使用 array_column 获取特定键的值数组,并将其最大化。

    max(array_column($array, 'bid'))
    

    【讨论】:

    • 我知道,但是如果我使用 array_search() 来查找相关密钥,我可能会以相同的出价获取错误的密钥,有没有办法避免这种情况?
    猜你喜欢
    • 2012-08-11
    • 2011-10-04
    • 1970-01-01
    • 2011-08-16
    • 2021-01-21
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多