There is a situation where the environment and conditions are used: the associated array in php, if the following array data: [php] $ arrayarray (array (& amp; #39; name & amp; #39; & gt; & quot; xiao & quot;, & amp; #39; age & amp; #39; & gt; 3 ), array (& amp; #3
Use environment and conditions
In this case, the associated array in php is as follows:
[Php]
$ Array = array (
Array (
'Name' => "xiao ",
'Age' => 3
),
Array (
'Name' => 'wang ',
'Age' => 1
),
Array (
'Name' => 'Chen ',
'Age' => 2
)
);
We need to sort the array's age fields. php's built-in functions, no matter which type of sort, obviously cannot meet our needs. Therefore, we can write a fast sorting code by ourselves, quickly implement our requirements
Notes
There is no pointer in php, so when we want to reference and pass, we cannot directly write quicksort (int * A, int begin, int end) like C code ), instead, we need to use the php & operator to pass the array address and the quick sorting function, so that we can implement reference transfer in php, instead of value transfer.
Quick sort code
[Php]
QuickSortProcess ($ array, 0, count ($ array)-1 );
Print_r ($ array );
/**
* Description: Gets the central point position in quick sorting.
*/
Function QuickPartition (& $ array, $ left, $ right ){
// 1. benchmark definition
$ Stand = $ array [$ left];
// 2. scan from both ends of the interval until $ left = $ right.
While ($ left <$ right ){
While ($ left <$ right & $ array [$ right] ['age'] >=$ stand ['age']) {
$ Right --;
}
If ($ left <$ right ){
$ Array [$ left ++] = $ array [$ right];
}
While ($ left <$ right & $ array [$ left] ['age'] <= $ stand ['age']) {
$ Left ++;
}
If ($ left <$ right ){
$ Array [$ right --] = $ array [$ left];
}
}
// 3. obtain the central point
$ Array [$ left] = $ stand;
Return $ left;
}
/**
* Description: main flow function for quick sorting
*/
Function QuickSortProcess (& $ array, $ begin, $ end ){
// 1. variable definition
$ Centers = NULL; // central point
If ($ begin <$ end ){
$ Partition = QuickPartition ($ array, $ begin, $ end );
QuickSortProcess ($ array, $ begin, $ sort-1 );
QuickSortProcess ($ array, $ rows + 1, $ end );
}
}
I used this fast sorting in the project. it was very happy. I did not waste the c code that took N days for the October 1 holiday to quickly sort the AC.