PHP排序数组按第3个字段从最大到最小,没有命名键

时间:2018-04-24 09:10:38

标签: php sorting

我使用说明制作了一个数组,现在有以下内容: -

Array ( 
    [0] => Array ( 
        [0] => Argentina 
        [1] => AR 
        [2] => 20.1 
    ) 
    [1] => Array ( 
        [0] => Austria 
        [1] => AT 
        [2] => 20.1 
    ) 
    [2] => Array ( 
        [0] => Australia 
        [1] => AU 
        [2] => 20.1 
    ) 
    [3] => Array ( 
        [0] => Azerbaijan 
        [1] => AZ 
        [2] => 20.1 
    ) 
    [4] => Array ( 
        [0] => Bosnia and Herzegovina 
        [1] => BA 
        [2] => 20.1 
    ) 
    [5] => Array ( 
        [0] => Barbados 
        [1] => BB 
        [2] => 20.1 
    ) 
    [6] => Array ( 
        [0] => Bangladesh 
        [1] => BD 
        [2] => 20.1 
    ) 
    [7] => Array ( 
        [0] => Belgium 
        [1] => BE 
        [2] => 20.1 
    ) 
    [8] => Array ( 
        [0] => Burkina Faso 
        [1] => BF 
        [2] => 20.1 
    ) 
    ........so on 
) 

我现在需要能够根据数值对这个最大到最小的排序。我已经检查了但是当我没有命名密钥时,我找不到办法吗?

先谢谢

1 个答案:

答案 0 :(得分:0)

获取最大值:

$value = max($array);

使用以下方法获取相应的密钥:

$key = array_search($value, $array);

以降序获取数组值 -

usort($yourArrayName, function ($item1, $item2) {
    return $item2['value'] <=> $item1['value'];
});

按升序排列 -

usort($yourArrayName, function ($item1, $item2) {
    return $item1['value'] <=> $item2['value'];
});