This function is able to sort an array based on another array that contains the order of occurrence. The values that are not present will be transferred into the end of the resultant.
Questa funzione permette di ordinare i valori di un array ($tosort) basandosi sui valori contenuti in un secondo array ($base), i valori non trovati verranno posizionati alla fine dell'ordinamento senza un'ordine specifico.
<?
$base= array('one', 'two', 'three');
$tosort= array('a'=>'two', 'b'=>'three', 'c'=>'five', 'd'=>'one', 'and'=>'four', 'f'=>'one');
uasort($tosort, function($key1, $key2) use ($base) {
$a1=array_search($key1, $base);
$a2=array_search($key2, $base);
if ( $a1===false && $a2===false ) { return 0; }
else if ( $a1===false && $a2 !== false) { return 1; }
else if ( $a1!==false && $a2 === false) {return -1;}
if( $a1 > $a2 ) { return 1; }
else if ( $a1 < $a2 ) { return -1; }
else if ( $a1 == $a2 ) { return 0; }
});
var_dump($tosort);
/*
the resulting of $tosort
array
'd' => string 'one' (length=3)
'f' => string 'one' (length=3)
'a' => string 'two' (length=3)
'b' => string 'three' (length=3)
'c' => string 'five' (length=6)
'e' => string 'four' (length=7)
*/
Gabriel
?>