简体   繁体   English

PHP:排序二维数组

[英]PHP : Sort 2 dimension Array

I'm trying to sort a two dimensions array, and I have no idea where to start. 我正在尝试对二维数组进行排序,而且我不知道从哪里开始。 I looked at array_multisort, but I don't really found a good solution with this sorting. 我看了一下array_multisort,但是用这种排序我没有真正找到一个好的解决方案。

I need to sort by time, each time are associate with a race. 我需要按时间排序,每次都与比赛相关。 I need to find who are the best 5 person so the best time. 我需要找到谁是最好的5个人,因此是最好的时间。

My array looks like this: 我的数组如下所示:

 [0]=>
  array(2) {
    [0]=>
    string(15) "Beaumier Mélina"
    [1]=>
    string(7) "1:29.30"
  }
  [1]=>
  array(2) {
    [0]=>
    string(14) "Frizzle Émilie"
    [2]=>
    string(7) "1:47.96"
  }
  [2]=>
  array(3) {
    [0]=>
    string(18) "Morissette Camélia"
    [2]=>
    string(7) "1:50.26"
    [1]=>
    string(7) "1:50.97"
  }

You can use usort . 您可以使用usort You give it a callback function and compare each index of the array. 您给它一个回调函数,并比较数组的每个索引。 Since you make the callback function you can compare by the time for each index in the array. 由于您创建了回调函数,因此可以按时间比较数组中每个索引的时间。

http://php.net/usort http://php.net/usort

From the above documentation: 从以上文档中:

<?php
function cmp($a, $b)
{
    return strcmp($a["fruit"], $b["fruit"]);
}

$fruits[0]["fruit"] = "lemons";
$fruits[1]["fruit"] = "apples";
$fruits[2]["fruit"] = "grapes";

usort($fruits, "cmp");
?>

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

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