|
|

|
(PHP 4 >= 4.0b4) array_multisort -- Sort multiple or multi-dimensional arrays Descriptionbool array_multisort (array ar1 [, mixed
arg [, mixed
... [, array
...]]])
Array_multisort() can be used to sort several
arrays at once or a multi-dimensional array according by one of
more dimensions. It maintains key association when sorting.
The input arrays are treated as columns of a table to be sorted
by rows - this resembles the functionality of SQL ORDER BY
clause. The first array is the primary one to sort by. The rows
(values) in that array that compare the same are sorted by the
next input array, and so on.
The argument structure of this function is a bit unusual, but
flexible. The very first argument has to be an
array. Subsequently, each argument can be either an array or a
sorting flag from the following lists.
Sorting order flags:
Sorting type flags:
SORT_REGULAR - compare items normally SORT_NUMERIC - compare items numerically SORT_STRING - compare items as strings
No two sorting flags of the same type can be specified after each
array. The sortings flags specified after an array argument apply
only to that array - they are reset to default SORT_ASC and
SORT_REGULAR after before each new array argument.
Returns TRUE on success, FALSE
on failure.
Esempio 1. Sorting multiple arrays
$ar1 = array ("10", 100, 100, "a");
$ar2 = array (1, 3, "2", 1);
array_multisort ($ar1, $ar2);
|
|
In this example, after sorting, the first array will contain 10,
"a", 100, 100. The second array will contain 1, 1, 2, "3". The
entries in the second array corresponding to the identical
entries in the first array (100 and 100) were sorted as well.
Esempio 2. Sorting multi-dimensional array
$ar = array (array ("10", 100, 100, "a"), array (1, 3, "2", 1));
array_multisort ($ar[0], SORT_ASC, SORT_STRING,
$ar[1], SORT_NUMERIC, SORT_DESC);
|
|
In this example, after sorting, the first array will contain 10,
100, 100, "a" (it was sorted as strings in ascending order), and
the second one will contain 1, 3, "2", 1 (sorted as numbers, in
descending order).
| User Contributed Notes: array_multisort |
kudos@telusplanet.net
25-Aug-2000 06:06 |
Running PHP 4.0.0 I had to do this to get the function to work:
array_multisort(&$array1, SORT_DESC, &$array2, &$array3);
in order to get this to return the sorted array(s).
|
|
e.deal@asmis.net
14-Dec-2000 06:05 |
attention, si une case d'un tableau à 2 dimension ne contient rien, alors le tri se fait mal
exemple:
$TAB[0][0]="TOTO";
$TAB[1][0]="toto";
$TAB[0][1]="TITI";
$TAB[1][1]="titi";
$TAB[0][2]="TATA";
$TAB[1][2]="";
reset($TAB);
while (list($i,$val)=each($TAB)){
while (list($j,$vale)=each($TAB[$i])) echo "TAB[$i][$j]=$vale ";
}
echo "---- ";
array_multisort ($TAB[0],SORT_ASC ) ;
reset($TAB);
while (list($i,$val)=each($TAB)){
while (list($j,$vale)=each($TAB[$i])) echo "TAB[$i][$j]=$vale ";
}
Ce qui donne l'affichage:
TAB[0][0]=TOTO
TAB[0][1]=TITI
TAB[0][2]=TATA
TAB[1][0]=toto
TAB[1][1]=titi
TAB[1][2]=
----
TAB[0][0]=TATA
TAB[0][1]=TITI
TAB[0][2]=TOTO
TAB[1][0]=toto
TAB[1][1]=titi
TAB[1][2]=
|
|
infong@darkflyer.de
12-Jan-2001 08:30 |
hi folks,
i have a simply sortfunction:
<pre>
<?
/* Anlegen eines Mehrdimensionales Array */
$ARTIKEL{"52608"} = array ("Name" => "Festplatte HD1", "Preis" => "999,50","Menge"=>"2");
$ARTIKEL{"34256"} = array ("Name" => "Grafikkarte Monster D3", "Preis" => "1395,99","Menge"=>"4");
$ARTIKEL{"99999"} = array ("Name" => "festplattenkontroller", "Preis" => "1999,49","Menge"=>"44");
$ARTIKEL{"34257"} = array ("Name" => "Grafikkarte Monster D2", "Preis" => "1999,99","Menge"=>"25");
/* Im 1. Durchgang nach Artikelnummer sortieren */
echo "Sortiert nach: Artikelnummer\n\n";
/* Wir sortieren das ARRAY nach Artikelnummer */
ksort ($ARTIKEL);
/* ARRAY ausgeben */
GEBE_MEIN_ARRAY_ZURUECK($ARTIKEL);
/* Im 2. Durchgang sortieren ARRAY nach Preis sortieren */
echo "Sortiert nach: Artikelpreis\n\n";
/* $FELD = Gibt an welches Feld im ARRAY sortiert werden soll */
$FELD="Preis";
/* Wir sortieren das ARRAY nach Preis */
usort ($ARTIKEL, MEINE_SORTIER_FUNKTION);
/* ARRAY ausgeben */
GEBE_MEIN_ARRAY_ZURUECK($ARTIKEL);
/* Im 3. Durchgang sortieren ARRAY nach Name sortieren */
echo "Sortiert nach: Artikelname\n\n";
/* $FELD = Gibt an welches Feld im ARRAY sortiert werden soll */
$FELD="Name";
/* Wir sortieren das ARRAY nach Preis */
usort ($ARTIKEL, MEINE_SORTIER_FUNKTION);
/* ARRAY ausgeben */
GEBE_MEIN_ARRAY_ZURUECK($ARTIKEL);
/* Im 4. Durchgang sortieren ARRAY nach Menge sortieren */
echo "Sortiert nach: Artikelmenge\n\n";
/* $FELD = Gibt an welches Feld im ARRAY sortiert werden soll */
$FELD="Menge";
/* Wir sortieren das ARRAY nach Preis */
usort ($ARTIKEL, MEINE_SORTIER_FUNKTION);
/* ARRAY ausgeben */
GEBE_MEIN_ARRAY_ZURUECK($ARTIKEL);
/* Sortierfunktion */
function MEINE_SORTIER_FUNKTION ($a, $b) {
/* $FELD soll uns in dieser Funktion zur Verfuegung stehen */
GLOBAL $FELD;
return strnatcasecmp($a[$FELD],$b[$FELD]);
}
/* Arrayausgabefunktion */
function GEBE_MEIN_ARRAY_ZURUECK($TRASH){
while ( list($key, $value) = each($TRASH) ) {
echo $TRASH{$key}{"Name"}." -> ".$TRASH{$key}{"Preis"}." -> ".$TRASH{$key}{"Menge"}." ";
}
echo "\n\n";
}
?>
</pre>
|
|
mnino@insight.com
18-Apr-2001 01:53 |
I cut-&-pasted the code from above and added some print_r() calls. The function array_multisort() doesn't work as adverstised. :] It generates an error: Warning: Argument 3 to array_multisort() is expected to be an array in array_multisort.php on line 9
Here is the PHP file array_multisort.php:
<pre>
<?php
$ar = array (array ("10", 100, 100, "a"), array (1, 3, "2", 1));
print_r($ar);
array_multisort ($ar[0], SORT_ASC, SORT_STRING,
$ar[1], SORT_NUMERIC, SORT_DESC);
print_r($ar);
?>
</pre>
Can anyone assist?
|
|
| |