-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Closed
Description
Description
I would like the callback of uasort()
(and usort()
) to receive the corresponding keys as well. A bit a like the callback of array_walk()
for example. Such that the callbacks definition becomes:
callback(mixed $a, mixed $b, mixed $aKey, mixed $bKey): int
My use case is that I get data from two databases and I want to sort one of those arrays using information of the second array. Now I have to preprocess the data to become one array:
$scores = [337 => 8, 646 => 7, 835 => 3, ...];
$dates = [337 => '2023-06-08 06:49:09', 646 => '2023-06-08 05:36:40', 835 => '2023-06-07 06:59:18', ...];
$singleDataSource = [];
foreach ($scores as $identifier => $score) {
$singleDataSource[$identifier] = [
'score' => $score,
'date' => $dates[$identifier],
];
}
uasort($singleDataSource, function(array $aData, array $bData) {
// calculate using score and date
});
Where ideally I want to do:
$scores = [337 => 8, 646 => 7, 835 => 3, ...];
$dates = [337 => '2023-06-08 06:49:09', 646 => '2023-06-08 05:36:40', 835 => '2023-06-07 06:59:18', ...];
uasort($scores, function(int $aScore, int $bScore, int $aIdentifier, int $bIdentifier) use ($dates) {
$aDate = $dates[$aIdentifier];
$bDate = $dates[$bIdentifier];
// calculate using score and date
});