从数组键php获取最大的numbre

从数组键php获取最大的numbre,php,arrays,Php,Arrays,我有这些数组,我需要做的是得到单独的数组,检查[total],然后得到最大的5个数字。我一直在努力,但我的头快要爆炸了,请帮帮我 Array ( [0] => Array ( [id] => 50 [faceid] => 1508653983 [fname] => Mario [lname] => Zelaya [email] => Email [handicap] => Handicap [province] => Province [coun

我有这些数组,我需要做的是得到单独的数组,检查[total],然后得到最大的5个数字。我一直在努力,但我的头快要爆炸了,请帮帮我

Array ( 
[0] => Array ( [id] => 50 [faceid] => 1508653983 [fname] => Mario [lname] => Zelaya [email] => Email [handicap] => Handicap [province] => Province [country] => Country [gender] => male [hand] => [shot1] => Shot #1 [shot2] => Shot #2 [shot3] => Shot #3 [shot4] => Shot #4 [shot5] => Shot #5 [news] => 1 [total] => 0 )

[1] => Array ( [id] => 49 [faceid] => 1665349411 [fname] => Yair [lname] => Villar [email] => servidorv@gmail.com [handicap] => lefthanded [province] => Buenos Aires [country] => Argentina [gender] => male [hand] => RH [shot1] => 200 [shot2] => 98 [shot3] => 98 [shot4] => 98 [shot5] => 98 [news] => 1 [total] => 592 ) 

[2] => Array ( [id] => 48 [faceid] => 1665349411 [fname] => Yair [lname] => Villar [email] => servidorv@gmail.com [handicap] => lefthanded [province] => Buenos Aires [country] => Argentina [gender] => male [hand] => RH [shot1] => 500 [shot2] => 250 [shot3] => 80 [shot4] => 30 [shot5] => 20 [news] => 1 [total] => 88000 )

) 

如何使用php实现这些功能。请帮忙

尝试使用php的usort函数对数组进行排序:

Array ( 
[0] => Array ( [id] => 50 [faceid] => 1508653983 [fname] => Mario [lname] => Zelaya [email] => Email [handicap] => Handicap [province] => Province [country] => Country [gender] => male [hand] => [shot1] => Shot #1 [shot2] => Shot #2 [shot3] => Shot #3 [shot4] => Shot #4 [shot5] => Shot #5 [news] => 1 [total] => 0 )

[1] => Array ( [id] => 49 [faceid] => 1665349411 [fname] => Yair [lname] => Villar [email] => servidorv@gmail.com [handicap] => lefthanded [province] => Buenos Aires [country] => Argentina [gender] => male [hand] => RH [shot1] => 200 [shot2] => 98 [shot3] => 98 [shot4] => 98 [shot5] => 98 [news] => 1 [total] => 592 ) 

[2] => Array ( [id] => 48 [faceid] => 1665349411 [fname] => Yair [lname] => Villar [email] => servidorv@gmail.com [handicap] => lefthanded [province] => Buenos Aires [country] => Argentina [gender] => male [hand] => RH [shot1] => 500 [shot2] => 250 [shot3] => 80 [shot4] => 30 [shot5] => 20 [news] => 1 [total] => 88000 )

) 
function cmp($a, $b)
{
    if ($a['total'] == $b['total'])
        return 0;

    return ($a['total'] > $b['total']) ? -1 : 1;
}

usort($yourarray, "cmp");

if (count($yourarray) > 5)
    $sortedArray = array_slice($yourarray, 0, 5);
else
    $sortedArray = $yourarray;

您将得到一个包含5个得分最高的元素的数组。如果输入数组中的元素少于5个,则最终的元素数与输入数组中的元素数相同。

尝试使用php的usort函数对数组进行排序:

function cmp($a, $b)
{
    if ($a['total'] == $b['total'])
        return 0;

    return ($a['total'] > $b['total']) ? -1 : 1;
}

usort($yourarray, "cmp");

if (count($yourarray) > 5)
    $sortedArray = array_slice($yourarray, 0, 5);
else
    $sortedArray = $yourarray;
function getTop5(array $data, $high2low = true)
{
    $total = array();

    foreach ($data as $val)
        $total[$val["id"]] = $val["total"];

    asort($total);
    return $high2low ? array_reverse($total) : $total;
}

$data = array(
        array("id" => 1, "total" => 25),
        array("id" => 2, "total" => 32),
        array("id" => 3, "total" => 21),
        array("id" => 4, "total" => 28)
        );

print_r(getTop5($data));

您将得到一个包含5个得分最高的元素的数组。如果您的输入数组中的元素少于5个,那么您最终得到的元素数与您的输入数组中的元素数相同。

嘿,不要让别人告诉您左手是一种障碍!嗯,我正在尝试构建一个函数来取消设置我不需要的所有内容,只保留[total]和[userid],然后尝试比较[total]并对数组desc进行排序,然后只打印最后5个。不要让别人告诉你左手是个障碍!嗯,我正在尝试构建一个函数来取消设置我不需要的所有内容,只保留[total]和[userid],然后尝试比较[total]并对数组描述进行排序,然后只打印最后5个,非常感谢你:)你让我开心了lol,现在的问题是它只返回总数没有id:(lol,但我会解决它:)谢谢,真的很好help@YairVillar欢迎你。。。你可以使用array_keys()来获取这个数组的密钥,你知道这些密钥就是id…完美兄弟,非常感谢你:)你让我的日子过得很开心,现在问题是它只返回总数没有id:(哈哈,但我会想出来:)谢谢,真的很好help@YairVillar欢迎你。。。您可以使用array_keys()获取此数组的键,并且您知道这些键就是ID…您可以将
cmp()
缩短为
return$b['total]-$a['total']
。同意,但为了清楚起见,cmp的扩展版本更容易理解:)您可以将
cmp()
缩短为
return$b['total']-$a['total']
。同意,但为了清晰起见,cmp的扩展版本更容易理解:)
function getTop5(array $data, $high2low = true)
{
    $total = array();

    foreach ($data as $val)
        $total[$val["id"]] = $val["total"];

    asort($total);
    return $high2low ? array_reverse($total) : $total;
}

$data = array(
        array("id" => 1, "total" => 25),
        array("id" => 2, "total" => 32),
        array("id" => 3, "total" => 21),
        array("id" => 4, "total" => 28)
        );

print_r(getTop5($data));