È possibile ordinare un array multidimensionale ma senza mantenere l'associazione dell'indice numerico.array_multisort con mantenimento dell'associazione dell'indice numerico
Come mantenere l'associazione dell'indice numerico?
CODICE:
$waiters[76] = array('weight' => 67, 'specialties' => 1);
$waiters[14] = array('weight' => 41, 'specialties' => 2);
$waiters[58] = array('weight' => 85, 'specialties' => 3);
$waiters[89] = array('weight' => 98, 'specialties' => 4);
$waiters[68] = array('weight' => 86, 'specialties' => 5);
$waiters[31] = array('weight' => 13, 'specialties' => 6);
print_r($waiters);
// Obtain a list of waiters
foreach ($waiters as $id => $waiter) {
$weight[$id] = $waiter['weight'];
$specialties[$id] = $waiter['specialties'];
}
// Sort the data with weight descending, specialties ascending
// Add $data as the last parameter, to sort by the common key
array_multisort(
$weight, SORT_DESC, SORT_NUMERIC,
$specialties, SORT_ASC, SORT_NUMERIC,
$waiters
);
print_r($waiters);
USCITA:
Array
(
[0] => Array
(
[weight] => 98
[specialties] => 4
)
[1] => Array
(
[weight] => 86
[specialties] => 5
)
[2] => Array
(
[weight] => 85
[specialties] => 3
)
[3] => Array
(
[weight] => 67
[specialties] => 1
)
[4] => Array
(
[weight] => 41
[specialties] => 2
)
[5] => Array
(
[weight] => 13
[specialties] => 6
)
)
output desiderato:
Array
(
[89] => Array
(
[weight] => 98
[specialties] => 4
)
[68] => Array
(
[weight] => 86
[specialties] => 5
)
[58] => Array
(
[weight] => 85
[specialties] => 3
)
[76] => Array
(
[weight] => 67
[specialties] => 1
)
[14] => Array
(
[weight] => 41
[specialties] => 2
)
[31] => Array
(
[weight] => 13
[specialties] => 6
)
)
@Naruto L'eventuale duplicazione sta usando 'usort 'ma sto usando' array_multisort' – itsazzad
Crea invece la tua funzione utente, che risolverebbe il tuo problema e renderebbe la domanda uguale. – vogomatix
@vogomatix Si prega di controllare la domanda modifica. La vecchia domanda stava facendo il duplicato. Ora non dovrebbe essere duplicato. – itsazzad