diff --git a/reference/array/functions/array-multisort.xml b/reference/array/functions/array-multisort.xml index d22abc8ed0..e8747b692a 100644 --- a/reference/array/functions/array-multisort.xml +++ b/reference/array/functions/array-multisort.xml @@ -1,5 +1,5 @@ - + @@ -8,17 +8,20 @@ Description - - boolarray_multisort - arrayar1 - mixedarg - mixed... - array... - - + + boolarray_multisort + arrayar1 + mixedarg + mixed... + array... + + + + &return.success; + array_multisort can be used to sort several - arrays at once or a multi-dimensional array according by one of + arrays at once or a multi-dimensional array according to one of more dimensions. Associative (string) keys are maintained while numerical keys are re-indexed. @@ -31,18 +34,18 @@ 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. + flexible. The 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: - SORT_ASC - sort in ascending order + SORT_ASC - Sort in ascending order - SORT_DESC - sort in descending order + SORT_DESC - Sort in descending order @@ -50,13 +53,13 @@ Sorting type flags: - SORT_REGULAR - compare items normally + SORT_REGULAR - Compare items normally - SORT_NUMERIC - compare items numerically + SORT_NUMERIC - Compare items numerically - SORT_STRING - compare items as strings + SORT_STRING - Compare items as strings @@ -66,9 +69,7 @@ only to that array - they are reset to default SORT_ASC and SORT_REGULAR before each new array argument. - - &return.success; - + Sorting multiple arrays @@ -78,17 +79,37 @@ $ar1 = array("10", 100, 100, "a"); $ar2 = array(1, 3, "2", 1); array_multisort($ar1, $ar2); + +var_dump($ar1); +var_dump($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. + + + string(2) "10" + [1]=> string(1) "a" + [2]=> int(100) + [3]=> int(100) +} +array(4) { + [0]=> int(1) + [1]=> int(1) + [2]=> string(1) "2" + [3]=> int(3) +} +]]> + - - 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. - + Sorting multi-dimensional array @@ -101,14 +122,151 @@ array_multisort($ar[0], SORT_ASC, SORT_STRING, ?> ]]> + + In this example, after sorting, the first array will contain "10", + 100, 100, "a" (it was sorted as strings in ascending order). The + second will contain 1, 3, "2", 1 (sorted as numbers, in + descending order). + + + array(4) { + [0]=> string(2) "10" + [1]=> int(100) + [2]=> int(100) + [3]=> string(1) "a" + } + [1]=> array(4) { + [0]=> int(1) + [1]=> int(3) + [2]=> string(1) "2" + [3]=> int(1) + } +} +]]> + + - 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). + + Sorting database results + + For this example, each element in the data + array represents one row in a table. This type of dataset is typical + of database records. + + + Example data: + + + + + + The data as an array, called data. This would usually, + for example, be obtained by looping with mysql_fetch_assoc. + + + 67, 'edition' => 2); +$data[] = array('volume' => 86, 'edition' => 1); +$data[] = array('volume' => 85, 'edition' => 6); +$data[] = array('volume' => 98, 'edition' => 2); +$data[] = array('volume' => 86, 'edition' => 6); +$data[] = array('volume' => 67, 'edition' => 7); +?> +]]> + + + In this example, we will order by volume descending, + edition ascending. + + + We have an array of rows, but array_multisort + requires an array of columns, so we use the the below code to obtain the + columns, then perform the sorting. + + + $row) { + $volume[$key] = $row['volume']; + $edition[$key] = $row['edition']; +} + +// Sort the data with volume descending, edition ascending +// Add $data as the last parameter, to sort by the common key +array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data); +?> +]]> + + + The dataset is now sorted, and will look like this: + + + + + + + + + Case insensitive sorting + + Both SORT_STRING and + SORT_REGULAR are case sensitive, strings + starting with a capital letter will come before strings starting + with a lowercase letter. + + + To perform a case insensitve search, we can sort by an all lowercase array. + + + + + &example.outputs; + + Alpha + [1] => atomic + [2] => bank + [3] => Beta +) +]]> + + + +