2014-05-19 9 views
5

Potrafię sortować tablicę wielowymiarową, ale bez zachowania powiązania indeksu numerycznego.array_multisort z zachowaniem powiązania indeksu numerycznego

Jak mogę zachować powiązanie indeksu numerycznego?

KOD:

$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); 

wyjściowych:

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 
     ) 

) 

sygnał wyjściowy:

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 
     ) 

) 
+0

@Naruto ewentualnego powielania używa 'usort 'ale używam' array_multisort' – itsazzad

+3

Zamiast tego stwórz własną funkcję użytkownika, która rozwiąże problem i sprawi, że pytanie będzie takie samo. – vogomatix

+0

@vogomatix Proszę sprawdzić pytanie. Stare pytanie powodowało, że był duplikatem. Teraz nie powinien być duplikatem. – itsazzad

Odpowiedz

0

do żądanej użytku wyjściowym ten kod:

<?php 
$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); 

//ksort($waiters); 
//$waiters = array_reverse($waiters, true); 
print_r($waiters); 
// Obtain a list of waiters 
foreach($waiters as $id=>$w){ 
    $w[$id] = $w['weight']; 
} 
    foreach ($waiters as $ii => $va) { 
     $sorter[$ii] = $va['weight']; 

    } 

    natcasesort($sorter); 
    foreach ($sorter as $ii => $va) { 
     $ret[$ii] = $waiters[$ii]; 
    } 

    echo "<pre>"; 
    $ret = array_reverse($ret, true); 
    print_r($ret); 

?> 
+1

Albo możesz po prostu zamieścić tutaj kod. – deceze

+0

wyjaśnić szczegółowo, w jaki sposób mogę opublikować mój kod tutaj –

+0

Dlaczego powinienem mieć, gdy istnieje sekcja pomocy dla niego? http://stackoverflow.com/editing-help#code – deceze

8
$keys = array_keys($waiters); 
array_multisort(
    $weight, SORT_DESC, SORT_NUMERIC, 
    $specialties, SORT_ASC, SORT_NUMERIC, 
    $waiters, $keys 
); 
$waiters = array_combine($keys, $waiters); 

lub użyj uasort

uasort(
    $data, 
    function ($some_data, $another_data) { 

     $result = 0; 

     if ($some_data['weight'] > $another_data['weight']) { 
      $result = -1; 
     } elseif ($some_data['weight'] < $another_data['weight']) { 
      $result = 1; 
     } elseif ($some_data['specialties'] > $another_data['specialties']) { 
      $result = 2; 
     } elseif ($some_data['specialties'] < $another_data['specialties']) { 
      $result = -2; 
     } 

     return $result; 

    } 
); 

ale wydajność uasort jest znacznie gorszy niż array_multisort

Powiązane problemy