Reputation: 1485
I have two arrays:
1- Id person (key) and qualification (value), this array have a descending order : arsort
Array
(
[61] => 02.30.00
[95] => 02.30.00
[19] => 02.01.00
[131] => 02.00.00
[58] => 01.60.00
[97] => 01.50.00
[76] => 01.40.00
[20] => 01.30.00
[112] => 01.10.00
[42] => 01.10.00
[116] => 01.04.00
}
2- ... and attempts associated to the Id person.
Array
(
[131] => 1
[58] => 1
[61] => 1
[112] => 2
[116] => 1
[42] => 1
[19] => 1
[20] => 1
[76] => 1
[97] => 1
[95] => 1
)
I need to maintain the descending order but adding ascending order by the number of the attempts. My problem is with these values:
[112] => 01.10.00 | 2
[42] => 01.10.00 | 1
How get this result?
Array
(
[61] => 02.30.00 // 1
[95] => 02.30.00 // 1
[19] => 02.01.00 // 1
[131] => 02.00.00 // 1
[58] => 01.60.00 // 1
[97] => 01.50.00 // 1
[76] => 01.40.00 // 1
[20] => 01.30.00 // 1
[42] => 01.10.00 // 1
[112] => 01.10.00 // 2
[116] => 01.04.00 // 1
)
Edit: ugly solution:
$new = array();
foreach($qualification as $k => $r)
{
$new[$k] = array(
'qualification'=> $r,
'attempt' => $attempt[$k],
'id' => $k,
);
}
foreach ($new as $key => $row)
{
$qlf[$key] = $row['qualification'];
$att[$key] = $row['attempt'];
}
array_multisort($qlf, SORT_DESC, $att, SORT_ASC, $new);
$result = array();
foreach ($new as $row)
{
$result[$row['id']] = $row['qualification'];
}
print_r($result);
Upvotes: 1
Views: 72
Reputation: 522342
uksort($qualification, function ($a, $b) use ($qualification, $attempt) {
return strcmp($qualification[$a], $qualification[$b])
?: $attempt[$a] - $attempt[$b];
});
I'm not entirely sure about which order you want to sort what in, but the above code will do; you just may have to switch $a
and $b
for reversing the order of one or the other.
Upvotes: 3