按3个字段对php数组排序

时间:2012-03-19 18:21:55

标签: php sorting

所以我需要在这个网站上实现排序:

http://news.bbc.co.uk/sport2/hi/olympics/medals_table/default.stm

我的阵列的一部分看起来像这样:

[1] => Array
    (
        [country_id] => 3
        [country_name] => Russia
        [gold] => 23
        [silver] => 21
        [bronze] => 28
        [total] => 72
    )

[2] => Array
    (
        [country_id] => 189
        [country_name] => USA
        [gold] => 36
        [silver] => 38
        [bronze] => 36
        [total] => 10
    )

[3] => Array
    (
        [country_id] => 230
        [country_name] => Germany
        [gold] => 16
        [silver] => 10
        [bronze] => 15
        [total] => 41
    )

[4] => Array
    (
        [country_id] => 231
        [country_name] => China
        [gold] => 51
        [silver] => 21
        [bronze] => 28
        [total] => 100
    )   

所以我需要按金,银,铜,然后是country_name对数组进行排序。

有没有人对此有所了解?

3 个答案:

答案 0 :(得分:1)

使用uasort功能:

$arr = array(
  Array(
    'country_id' => 3,
    'country_name' => 'Russia',
    'gold' => 23,
    'silver' => 21,
    'bronze' => 28,
    'total' => 72,
  ),
  Array(
    'country_id' => 189,
    'country_name' => 'USA',
    'gold' => 36,
    'silver' => 38,
    'bronze' => 36,
    'total' => 10,
  ),
  Array(
    'country_id' => 230,
    'country_name' => 'Germany',
    'gold' => 16,
    'silver' => 10,
    'bronze' => 15,
    'total' => 41,
  ),
  Array(
    'country_id' => 231,
    'country_name' => 'China',
    'gold' => 51,
    'silver' => 21,
    'bronze' => 28,
    'total' => 100,
  )
);

function mySort($a,$b) {
    if ($b['gold'] == $a['gold']) {
        if ($b['silver'] == $a['silver']) {
            if ($b['bronze'] == $a['bronze']) {
                return strcmp($b['country_name'], $a['country_name']);
            } else {
                return $b['bronze'] - $a['bronze'];
            }
        } else {
            return $b['silver'] - $a['silver'];
        }
    } else {
        return $b['gold'] - $a['gold'];
    }
}

uasort($arr, 'mySort');
print_r($arr);

<强>输出:

Array
(
[1] => Array
    (
        [country_id] => 189
        [country_name] => USA
        [gold] => 36
        [silver] => 38
        [bronze] => 36
        [total] => 10
    )

[0] => Array
    (
        [country_id] => 3
        [country_name] => Russia
        [gold] => 23
        [silver] => 21
        [bronze] => 28
        [total] => 72
    )

[3] => Array
    (
        [country_id] => 231
        [country_name] => China
        [gold] => 51
        [silver] => 21
        [bronze] => 28
        [total] => 100
    )

[2] => Array
    (
        [country_id] => 230
        [country_name] => Germany
        [gold] => 16
        [silver] => 10
        [bronze] => 15
        [total] => 41
    )

)

答案 1 :(得分:0)

我知道两个选项:

  1. usort()使用用户定义的比较函数按值对数组进行排序;
  2. ksort()按键排序数组。
  3. 希望这个答案足够有用。

答案 2 :(得分:0)

可能有更短的方法可以找到。但这也可能有所帮助

function my_sort($a, $b) {
    if($a['gold'] > $b['gold']) {
        return -1;
    }
    elseif ($a['gold'] == $b['gold']) {
        if($a['silver'] > $b['silver']) {
            return -1;
        }
        elseif ($a['silver'] == $b['silver']) {
          if ($a['bronze'] > $b['bronze']) {
              return -1;
            }
            elseif ($a['bronze'] == $b['bronze']) {
            return strcmp($b['country_name'], $a['country_name']);
           }
          return 1;
        }
      return 1;
    }
    return 1;
}

usort($arr, 'my_sort');

print_r($arr);