我有这个简单的足球队阵列:
Array
(
[0] => Array
(
[name] => MANCHESTER
[pts] => 8
[gd] => 5
)
[1] => Array
(
[name] => BOURNEMOUTH
[pts] => 3
[gd] => 2
)
[2] => Array
(
[name] => STOKE CITY
[pts] => 2
[gd] => 4
)
[3] => Array
(
[name] => LIVERPOOL
[pts] => 3
[gd] => 5
)
[4] => Array
(
[name] => ARSENAL
[pts] => 9
[gd] => 1
)
)
我想先按pts
对小组进行排序,如果我们有相同的pts
,请按gds
排序。
仅按我们的分数排序:
function sortByOrder($a, $b){
return $a['pts'] - $b['pts'];
}
usort($this_is_my_array, 'sortByOrder');
结果数组是:
ARSENAL (pts:9, gd:1)
MANCHESTER (pts:8, gd:5)
BOURNEMOUTH (pts:3, gd:2)
LIVERPOOL (pts:3, gd:5)
STOKE CITY (pts:2, gd:4)
但BOURNEMOUTH
& LIVERPOOL
我们重复pts
,因此我们需要获得此结果:
ARSENAL (pts:9, gd:1)
MANCHESTER (pts:8, gd:5)
LIVERPOOL (pts:3, gd:5)
BOURNEMOUTH (pts:3, gd:2)
STOKE CITY (pts:2, gd:4)
答案 0 :(得分:3)
尝试
uasort($your_array, function($a,$b){
$c = $b['pts'] - $a['pts'];
$c .= $b['gd'] - $a['gd'];
return $c;
});
print_r($your_array);
答案 1 :(得分:0)
试试这个:
$array=array(
array("name"=>"MANCHESTER","pts"=>"8","gd"=>"5"),
array("name"=>"BOURNEMOUTH","pts"=>"3","gd"=>"2"),
array("name"=>"STOKE CITY","pts"=>"2","gd"=>"4"),
array("name"=>"LIVERPOOL","pts"=>"3","gd"=>"5"),
array("name"=>"ARSENAL","pts"=>"9","gd"=>"1")
);
foreach ($array as $key => $row) {
$searchcountvalue[$key] = $row['name'];
$pastsearchcountvalue[$key] = $row['pts'];
$abstract_count[$key] = $row['gd'];
}array_multisort($pastsearchcountvalue, SORT_DESC, $abstract_count, SORT_DESC, $array);
echo "<pre>";print_r($array);
答案 2 :(得分:0)
此格式的数组,请尝试检查此解决方案。
$s[] = array
(
'name' => 'MANCHESTER',
'pts' => 8,
'gd' => 5
);
$s[] = array
(
'name' => 'BOURNEMOUTH',
'pts' => 3,
'gd' => 2
);
$s[] = array
(
'name' => 'STOKE CITY',
'pts' => 2,
'gd' => 4
);
$s[] = array
(
'name' => 'testing',
'pts' => 2,
'gd' => 6
);
$s[] = array
(
'name'=> 'LIVERPOOL',
'pts' => 3,
'gd' => 5
);
$s[] = array
(
'name' => 'ARSENAL',
'pts' => 9,
'gd' => 1
);
function array_orderby()
{
$args = func_get_args();
$data = array_shift($args);
foreach ($args as $n => $field) {
if (is_string($field)) {
$tmp = array();
foreach ($data as $key => $row)
$tmp[$key] = $row[$field];
$args[$n] = $tmp;
}
}
$args[] = &$data;
call_user_func_array('array_multisort', $args);
return array_pop($args);
}
$sorted = array_orderby($s, 'pts', SORT_DESC, 'gd', SORT_DESC);
// OutPut
Array
(
[0] => Array
(
[name] => ARSENAL
[pts] => 9
[gd] => 1
)
[1] => Array
(
[name] => MANCHESTER
[pts] => 8
[gd] => 5
)
[2] => Array
(
[name] => LIVERPOOL
[pts] => 3
[gd] => 5
)
[3] => Array
(
[name] => BOURNEMOUTH
[pts] => 3
[gd] => 2
)
[4] => Array
(
[name] => Testing
[pts] => 2
[gd] => 6
)
[5] => Array
(
[name] => STOKE CITY
[pts] => 2
[gd] => 4
)
)