note 117324 added to function.array-intersect
| From: | info at iridsystem dot it | Date: | Thu, 21 May 2015 18:16:52 +0000 |
| Subject: | note 117324 added to function.array-intersect | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-202634@lists.php.net to get a copy of this message | ||
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
?>
----
Server IP: 72.52.91.14
Probable Submitter: 79.59.237.76
----
Manual Page -- http://php.net/manual/en/function.array-intersect.php
Edit -- https://master.php.net/note/edit/117324
Del: integrated -- https://master.php.net/note/delete/117324/integrated
Del: useless -- https://master.php.net/note/delete/117324/useless
Del: bad code -- https://master.php.net/note/delete/117324/bad+code
Del: spam -- https://master.php.net/note/delete/117324/spam
Del: non-english -- https://master.php.net/note/delete/117324/non-english
Del: in docs -- https://master.php.net/note/delete/117324/in+docs
Del: other reasons-- https://master.php.net/note/delete/117324
Reject -- https://master.php.net/note/reject/117324
Search -- https://master.php.net/manage/user-notes.php