How PHP implements the ordering of two-dimensional associative arrays
For example, arrays are like this:
$example = Array (Array (' B ' =>7, ' RSD ' =>6, ' GDD ' =>3),
Array (' d ' =>5, ' ess ' =>8, ' FFS ' =>5),
Array (' C ' =>2, ' SDV ' =>5, ' VFS ' =>6),
Array (' A ' =>8, ' HDS ' =>4, ' RFS ' =>9));
Do not use PHP system functions, with Uasort () or Uksort (), how to write a custom function to pass in?
function Compare ($X, $Y)
So with Uasort ($example, ' compare '),
How should this compare be written?
Two-dimensional arrays that are not associative I know how to write compare,
function Compare ($x, $y)
{
if ($x [0] = = $y [0])
return 0;
ElseIf ($x [0] < $y [0])
return-1;
Else
return 1;
}
------Solution--------------------
Manual Array_multisort
See Example # #
------Solution--------------------
Example #4 Sorting the results of a database
In this example, each cell in the data array represents a row in a table. This is a data collection of typical database records.
The data in the example is as follows:
Volume
------Solution--------------------
Edition
-------+--------
67
------Solution--------------------
2
86
------Solution--------------------
1
85
------Solution--------------------
6
98
------Solution--------------------
2
86
------Solution--------------------
6
67
------Solution--------------------
7
The data is all stored in an array named data. This is usually done by looping the results from the database, such as MYSQL_FETCH_ASSOC ().
$data [] = Array (' volume ' = +, ' edition ' = 2);
$data [] = Array (' volume ' = +, ' edition ' = 1);
$data [] = Array (' volume ' = =, ' edition ' = 6);
$data [] = Array (' volume ' = = 98, ' edition ' = 2);
$data [] = Array (' volume ' = +, ' edition ' = 6);
$data [] = Array (' volume ' = +, ' edition ' = 7);
?>
In this example, the volume is sorted in descending order, and the edition in ascending order.
Now that you have an array with rows, but Array_multisort () requires an array of columns, so use the following code to get the columns and sort them.
Get a list of columns
foreach ($data as $key = = $row) {
$volume [$key] = $row [' volume '];
$edition [$key] = $row [' Edition '];
}