简体   繁体   English

从中的关联数组中获得最大值,如果相等,则以其他值最低获得最高值

[英]getting the highest value from an associative array in and if equal, get the highest with the lowest other value

Let me try and explain this as easy as possible. 让我尝试并尽可能简单地解释一下。 I have an array which contains the shipping fees of several products. 我有一个数组,其中包含几种产品的运费。

I have the value of 我的价值

Item ID Local Shipping International Shipping Additional Local Shipping Additional International Shipping 项目ID本地运输国际运输附加本地运输附加国际运输

To make it easier, I just need to know how to calculate the part of the local shipping. 为了简化操作,我只需要知道如何计算本地运费的一部分即可。 I can afterwards apply the same formula to the international shipping part. 之后,我可以将相同的公式应用于国际运输部分。

The array may contain 1 or 5 or 100 products, each with their own different (or same) amounts, like this: 该数组可以包含1或5或100种产品,每种产品具有各自不同(或相同)的数量,如下所示:

Array ( 

    [0] => Array ( 
        [item_id] => 788 
        [local_shipping] => 13.00 
        [intl_shipping] => 45.00 
        [addl_local_shipping] => 10.00 
        [addl_intl_shipping] => 20.00 
    )
    [1] => Array ( 
        [item_id] => 234 
        [local_shipping] => 23.00 
        [intl_shipping] => 5.00 
        [addl_local_shipping] => 1.00 
        [addl_intl_shipping] => 2.00 
    )
    [2] => Array ( 
        [item_id] => 543 
        [local_shipping] => 23.00 
        [intl_shipping] => 6.00 
        [addl_local_shipping] => 0.50 
        [addl_intl_shipping] => 5.00 
    )

) 

So, what I'm trying to get is the array that contains the HIGHEST local_shipping value, which in that case would be both [1] and [2] with "23.00". 因此,我要获取的是包含最高local_shipping值的数组,在这种情况下,该数组将是[1]和[2]都为“ 23.00”。

If there's only one unique highest value, I need it to return the "local_shipping" and the "addl_local_shipping" like 如果只有一个唯一的最大值,我需要它返回“ local_shipping”和“ addl_local_shipping”

Local Shipping is 23.00 and additional local shipping is 1.00

Now, if there's 2 arrays with common highest values, I need it to return the one with the LOWEST "addl_local_shipping" value, which in that case is [2] like: 现在,如果有2个具有共同最大值的数组,则我需要它返回一个具有最低“ addl_local_shipping”值的数组,在这种情况下,其值为[2]:

Local Shipping is 23.00 and additional local shipping is 0.50

Does that make sense? 那有意义吗?

I was able to get the highest "local_shipping" value out of the array with this: 我能够从数组中获得最高的“ local_shipping”值:

$max = 0;
foreach( $my_array as $k => $v )
{
$max = max( array( $max, $v['local_shipping'] ) );
}
echo "Local Shipping is " . $max;

But I have no idea how to figure out how to print the related "addl_local_shipping" string AND sort out the problem if there are multiple ones with an equal high value. 但是我不知道如何弄清楚如何打印相关的“ addl_local_shipping”字符串,如果有多个具有相同的高值的字符串,则无法解决问题。

You can use usort to sort the array based on local shipping descending and additional ascending, then take the values from the 1st result in the array: 您可以使用usort根据本地装运降序和其他升序对数组进行排序,然后从数组的第一个结果中获取值:

usort($array, function($a, $b){
    return $b['local_shipping'] - $a['local_shipping'] ?:
        $a['addl_local_shipping'] - $b['addl_local_shipping'];
});

echo 'local shipping: ' . $array[0]['local_shipping'];
echo 'additional local shipping: ' . $array[0]['addl_local_shipping'];

However, as noted in comments, if you are getting this data from an sql query, it makes more sense to let the database do the work for you with an ORDER BY clause. 但是,如注释中所述,如果要从sql查询中获取此数据,则让数据库使用ORDER BY子句为您完成工作更为有意义。 The result will be the same - the 1st element in the array will have the data you require 结果将相同-数组中的第一个元素将具有您所需的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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