[php-src] Issue #11585: Passing keys to uasort (and usort)

From: Date: Mon, 03 Jul 2023 21:18:52 +0000
Subject: [php-src] Issue #11585: Passing keys to uasort (and usort)
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-244871@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/11585 Author: lode ### 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: ```php 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: ```php $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: ```php $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 }); ```

« previous php.bugs (#244871) next »