2012-07-29 14 views
5

Ho la seguente matrice:tipi multipli in un array

Array 
(
[0] => Array 
    (
     [note] => test 
     [year] => 2011 
     [type] => football 
    ) 

[1] => Array 
    (
     [note] => test1 
     [year] => 2010 
     [type] => basket 
    ) 

[2] => Array 
    (
     [note] => test2 
     [year] => 2012 
     [type] => football 
    ) 

[3] => Array 
    (
     [note] => test3 
     [year] => 2009 
     [type] => basket 
    ) 

[4] => Array 
    (
     [note] => test4 
     [year] => 2010 
     [type] => football 
    ) 

) 

E desidero ordinare primo secondo un'altra matrice per tipo:

Ad esempio: $sort = array('football','basket');

e poi da anno.

Come posso farlo?

Grazie.

output desiderato dovrebbe essere:

Array 
(
[2] => Array 
    (
     [note] => test2 
     [year] => 2012 
     [type] => football 
    ) 
[0] => Array 
    (
     [note] => test 
     [year] => 2011 
     [type] => football 
    ) 
[4] => Array 
    (
     [note] => test4 
     [year] => 2010 
     [type] => football 
    ) 

[1] => Array 
    (
     [note] => test1 
     [year] => 2010 
     [type] => basket 
    ) 

[3] => Array 
    (
     [note] => test3 
     [year] => 2009 
     [type] => basket 
    ) 

) 

Non mi importa se ci Azzera i valori di indice.

Grazie.

risposta

2

Usa array_multisort. Supponendo che la matrice è $arr:

foreach($arr as $key=>$row) { 
    $type[$key] = $row['type']; 
    $year[$key] = $row['year']; 
} 
array_multisort($type, SORT_ASC, $year, SORT_ASC, $arr); 

Per utilizzare una matrice addictional specificando ordine tipo, si potrebbe fare: collegamento

$sortBy = array('football','basket'); 
foreach($arr as $key=>$row) { 
    $type[$key] = array_search($row['type'],$sortBy); 
    $year[$key] = $row['year']; 
} 
array_multisort($type, SORT_ASC, $year, SORT_ASC, $arr); 

Esempio:

http://codepad.org/qhZCpbZE

+0

Grazie amico sembra funzionare. Ma come posso includere anche l'array $ sortBy? – glarkou

+0

@salamis Ho modificato la mia risposta per coprire anche questo. – lafor

+0

Grazie compagno sembra funzionare come previsto. una domanda veloce Perché usiamo ancora '$ type, SORT_ASC' se stiamo ordinando da un altro array? – glarkou

1

Usa

function byType($a, $b) 
{ 
    if ($a['type'] == $b['type']) { 
     return 0; 
    } 

    foreach(array('football','basket') as $type) { 
     if ($a['type']=$type) return -1; 
     if ($b['type']=$type) return 1; 
    } 

    return 0; 
} 


function byYear($a, $b) 
{ 
    if ($a['year'] == $b['year']) { 
     return 0; 
    } 
    return ($a['year'] < $b['year']) ? -1 : 1; 
} 

usort($array,"byType"); 
usort($array,"byYear"); 
+0

Non è un compagno di lavoro. Prima di tutto perché non posso usare un altro array per ordinare per tipo. E in secondo luogo quando viene eseguito il secondo usort, tutto si mescola di nuovo. Per favore controlla: http://codepad.org/PO3CvGsu e il risultato desiderato. Mi piacerebbe che l'ordinamento per anno accadesse solo se il tipo è lo stesso. – glarkou

+0

Funziona. http://codepad.org/PwYKyyMS – Adi

+0

@Adnan Non funziona, cambia l'array, funziona attualmente per "fortuna" – Leigh

4

richiede una versione di PHP che supporta le chiusure quando viene eseguito così com'è

L'array secondario può essere definito al di fuori del tipo stesso. Se non è possibile supportare chiusure, questo può essere inserito direttamente nella routine di ordinamento.

$sortBy = array('football', 'basket'); 

Ordinamento di routine.

Ordina per anno se i tipi sono uguali e ordina i tipi nell'array $sortBy in caso contrario.

usort($a, function($a, $b) use ($sortBy) { 
    if ($a['type'] == $b['type']) { 
     return ($b['year'] - $a['year']); 
    } 
    $typeOrder = array_flip($sortBy); 
    return $typeOrder[$a['type']] - $typeOrder[$b['type']]; 
}); 
+0

Come posso usare questo? Ho provato http://codepad.org/HCodcfgl ma non sembra funzionare. Grazie per l'aiuto. – glarkou

+0

@salamis Codepad è PHP 5.2, che ha 6 anni e non supporta le chiusure. Quale versione di PHP ti ** usi **? (Puoi usare anche [il codificatore di Viper 7] (http://codepad.viper-7.com/MpTYaB)) – Leigh