|
|
 |
array_multisort (PHP 4 ) array_multisort -- Sortiert mehrere oder multidimensionale Arrays Beschreibungbool array_multisort ( array ar1 [, mixed arg [, mixed ... [, array ...]]])
array_multisort() wird zum Sortieren von
entweder mehreren Arrays auf einmal, oder eines
multidimensionalen Arrays (entsprechend einer von mehreren
Dimensionen) benutzt. Bei der Sortierung werden die
Schlüsselassoziationen beibehalten.
Die angegebenen Arrays werden als Spalten einer Tabelle
behandelt, welche nach Reihen sortiert werden - ähnlich der SQL
Klausel ORDER BY. Das erste Array ist auch das erste in der
Sortierreihenfolge. Die in diesem Array gleichen Zeilen (Werte)
werden anhand des nächsten angegebenen Arrays sortiert, usw.
Die Struktur der Argumente ist etwas ungewöhnlich, aber
flexibel. Das aller erste Argument muss ein Array sein. Die
nachfolgenden Argumente können entweder ein Array oder eines der
folgenden Sortierflags sein.
Flags für Sortierreihenfolge:
Flags für Sortiertypen:
SORT_REGULAR - vergleiche Felder normal SORT_NUMERIC - vergleiche Felder numerisch SORT_STRING - vergleiche Felder als Strings
Es kann nur ein Sortierflag des selben Typs nach jedem Array
spezifiziert werden. Sortierflags nach einem Array Argument
gelten nur für dieses Array, und werden vor jedem neuen Array
Argument zu den Defaultflags SORT_ASC und SORT_REGULAR
zurückgesetzt.
Gibt bei Erfolg TRUE zurück, im Fehlerfall FALSE.
Beispiel 1. Sortieren mehrerer Arrays $ar1 = array ("10", 100, 100, "a");
$ar2 = array (1, 3, "2", 1);
array_multisort ($ar1, $ar2); |
|
In diesem Beispiel enthält das erste Array nach dem Sortieren 10,
"a", 100, 100. Das zweite Array wird 1, 1, "2", 3 enthalten. Die
Einträge des zweiten Arrays, welche den identischen Einträgen des
ersten Arrays entsprechen (100 und 100) wurden ebenfalls
sortiert.
Beispiel 2. Sortieren eines mehrdimensionalen Arrays $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 diesem Beispiel enthält das erste Array nach dem Sortieren 10,
100, 100, "a" (wurde als Strings in aufsteigender Reihenfolge
sortiert), und das zweite enthält 1, 3, "2", 1 (numerisch und
absteigender Reihenfolge sortiert).
User Contributed Notes array_multisort |
 |
kudos@telusplanet.net
26-Aug-2000 12: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).
|
|
kat.n0spam@audiogalaxy.com
24-Aug-2001 04:25 |
|
if you're having problems with array_multisort changing variables in global
space when it is called inside a function and you're not passing in the
function parameters by reference you can alleviate the problem by calling
array_multisort with its parameters by reference, eg,
array_multisort(&$a, SORT_DESC, &$b); More details here in my bug
report:
http://www.php.net/bugs.php?id=12936
|
|
salim@codesplash.com
10-Oct-2001 08:41 |
|
If you have an array that contains output from a SQL SELECT statment and
you want to sort the array on a column, you have to create a separate array
containing the values of that column like so:
//Get table output into $rows array
$result = mysql_query("SELECT field1, field2 from table");
while ($rows[] = mysql_fetch_array($result)) {}
//Sort on field2
foreach ($rows as $val) {
$sortarray[] = $val['field2'];
}
array_multisort($rows, $sortarray);
//Now, $rows is sorted on field2
If anyone knows an easier way to do this, please let us know!
|
|
yann@nouvelle-vie.com
13-Feb-2002 08:23 |
|
If you want to sort a multidimentionnal array based on a certain row, the
easiest way I found was using usort() and a comparison function of my own.
The comparison function is called with two elements of the array. Then, in
the function, as the two elements are also arrays (we are dealing with a
multidimentionnal one), the comparison can be made on any row:
if ($a[2] > $b[2]) return 1; ...
See the definition of usort() for that.
|
|
richard@richardhayes.net
13-Mar-2002 12:33 |
|
EXAMPLE: You have two arrays where one has results from a test and the
other names of the participants and you need to graph these results in
descending order, but keeping the name and score relationship in tact.
<?
$scores=array(90, 10, 70, 100);
$participants=array("liam","val","john","richard");
array_multisort($scores,SORT_DESC,$participants);
# would now produce:
# $scores[0]=100; $participants[0]="richard";
# $scores[1]=90; $participants[1]="liam";
# etc..
?>
|
|
alex@spektrs.lv
27-May-2002 01:59 |
|
This example for sorting SQL results is very usefull, except the author
mixed up the array_multisort() arguments :
//Get table output into $rows array
$result = mysql_query("SELECT field1, field2 from table");
while ($rows[] = mysql_fetch_array($result)) {}
//Sort on field2
foreach ($rows as $val) {
$sortarray[] = $val['field2'];
}
array_multisort($sortarray,$rows);
//Now, $rows is sorted on field2
|
|
mike@linux-solutions.at
18-Jun-2002 06:45 |
|
it seems that you always have to provide the arrays to the funktion by
using &$arrayname. anything else didn't work for me....
regards
Mike
|
|
jkh1978@hotmail.com
21-Jul-2002 06:31 |
|
The original post is correct... not the second post on this code:
The code from If you have an array that contains output from a SQL SELECT
statment and
you want to sort the array on a column, you have to create a separate
array containing the values of that column like so:
//Get table output into $rows array
$result = mysql_query("SELECT field1, field2 from table");
while ($rows[] = mysql_fetch_array($result)) {}
//Sort on field2
foreach ($rows as $val) {
$sortarray[] = $val['field2'];
}
array_multisort($rows, $sortarray);
//Now, $rows is sorted on field2
If anyone knows an easier way to do this, please let us know!
|
|
kschnaitter@hotmail
24-Jul-2002 03:21 |
|
It can be very useful to use array_keys(*) as one of the arguments.
Example: you have an associative array of items and quantities, where the
item is used as a key. Say you want to sort the array by descending
quantity, and sort items with the same quantity in alphabetical order. Here
is the code:
$arr["potato"] = 5;
$arr["orange"] = 3;
$arr["apple"] = 3;
$arr["lemon"] = 4;
$arr["onion"] = 5;
$arr["grapefruit"] = 3;
array_multisort ($arr, SORT_DESC, array_keys ($arr));
print_r ($arr);
Here is the output:
Array
(
[onion] => 5
[potato] => 5
[lemon] => 4
[apple] => 3
[grapefruit] => 3
[orange] => 3
)
|
|
|
24-Jul-2002 09:23 |
|
the 13-Apr note by richard is really handy when you don't have database
acces/support and you're using multiple arrays instead. It can be used for
example to sort topics in a forum on 'last edited'. Make sure you have 2
arrays, one with the different topics (for example filenames) and one with
the 'last-edited times'
array_multisort($topicfilenames,SORT_DESC,$lasteditedtimes);
|
|
 |
 | |