排序2阵列来获得一个最高和最低的另一 [英] Sorting 2 arrays to get the highest in one and lowest in another

查看:129
本文介绍了排序2阵列来获得一个最高和最低的另一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个阵列。键重新present在游戏的玩家ID(和是在两个阵列是相同的)和再$ P $的值psents平中之一,在其他的得分。我所试图做的就是具有最高ping和得分最低的玩家ID(键)。我不能让我的头周围的任何,将做到这一点不爽。

I have 2 arrays. The keys represent the player id in a game (and is the same in both arrays) and the value represents the Ping in one and the score in the other. What I am trying to do is get the Player ID (key) that has the highest ping and the lowest score. I can't get my head around any of the sorts that would do this.

我没有使用2个数组,我只是不知道该怎么做。

I don't have to use 2 arrays, I just don't know how else to do it.

感谢。

推荐答案

现场演示 HTTP://$c$cpad.org/46m3mHIH

排号这种类型的结构会更好地工作......

Arranging this type of architecture would work better...

$players = array(
    array(
        "name" => "l337 H4x0r",
        "score" => 10432,
        "ping" => 0.35
    ),
    array(
        "name" => "El Kabooom",
        "score" => 19918,
        "ping" => 0.45
    ),
    array(
        "name" => "Kapop",
        "score" => 10432,
        "ping" => 0.38
    )
);

然后,你可以更有效地排序的多维数组和检索你的 $ lowestScore $ highestPing 值。

$playersScore = subval_sort($players,'score'); 
$lowestScore = $playersScore[0]['score'];

$playersPing = subval_sort($players,'ping'); 
$HighestPing = $playersPing[ count($players)-1 ]['score'];

function subval_sort($a,$subkey) {
    foreach($a as $k=>$v) {
        $b[$k] = strtolower($v[$subkey]);
    }
    asort($b);
    foreach($b as $key=>$val){
        $c[] = $a[$key];
    }
    return $c;
}

这篇关于排序2阵列来获得一个最高和最低的另一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆