2016-11-23 5 views
0

Ich benutze array_multisort wie immer, aber dieses Mal hat es Probleme beim Sortieren richtig.array_multisort funktioniert nicht richtig?

ich es verwenden, um ein mehrdimensionales Array zu sortieren ($ data), aber ich vereinfacht das Problem in diesem Beispiel:

$data = array(6 => 'WEEK 48', 7 => 'WEEK 49', 8 => 'WEEK 47', 9 => 'WEEK 50', 10 => 'WEEK 51'); 

    $sort = array(8 => 201647, 6 => 201648, 7 => 201649, 9 => 201650, 10 => 201651); 

    array_multisort($sort, SORT_ASC, $data); 

    Output: 
    Array 
    (
     [0] => WEEK 48 
     [1] => WEEK 49 
     [2] => WEEK 47 
     [3] => WEEK 50 
     [4] => WEEK 51 
    ) 

Was bin ich?

+1

Scheint Ihre Eingabeparameter werden iCorrect http://www.w3schools.com/Php/func_array_multisort.asp Sie $ sort Array Sortierung anstelle von $ data – VadimB

Antwort

0

Dumme mir, es war ein ksort($data) zwischen Linien, die die Sortierung vermasselt.

1

Versuchen Sie, diese

<?php 

     $data = array(6 => 'WEEK 48', 7 => 'WEEK 49', 8 => 'WEEK 47', 9 => 'WEEK 50', 10 => 'WEEK 51'); 

     $sort = array(8 => 201647, 6 => 201648, 7 => 201649, 9 => 201650, 10 => 201651); 

     array_multisort($data, SORT_ASC, $sort); 

     echo '<pre>'; 
     print_r($data); 

    ?> 

Ausgang:

Array 
(
    [0] => WEEK 47 
    [1] => WEEK 48 
    [2] => WEEK 49 
    [3] => WEEK 50 
    [4] => WEEK 51 
) 
+0

Ich brauche $ Art der Sortierung Array zu sein, da die Daten $ ist in der Tat ein mehrdimensionales Array. Warum funktioniert es nicht umgekehrt? –

0

Syntax

array_multisort(array1,sorting order,sorting type,array2,array3...) 

Sie sollten sorting_type für Ihr Array liefern

array_multisort($sort, SORT_ASC, SORT_STRING, $data, SORT_ASC, SORT_NUMERIC); 

var_dump($sort); 
var_dump($data); 

Ergebnisse:

array(5) { 
    [0]=> int(201647) 
    [1]=> int(201648) 
    [2]=> int(201649) 
    [3]=> int(201650) 
    [4]=> int(201651) 
} 

array(5) { 
    [0]=> string(7) "WEEK 47" 
    [1]=> string(7) "WEEK 48" 
    [2]=> string(7) "WEEK 49" 
    [3]=> string(7) "WEEK 50" 
    [4]=> string(7) "WEEK 51" 
} 
+0

$ Daten sind immer noch nicht richtig sortiert. –

+0

Ich habe die Antworten aktualisiert = _ = ich habe 'sorting_type' für' $ data' vergessen – Doanh

+0

opps, fehlende 'sorting_order' nicht' sorting_type', Sorry – Doanh