Use environment and Conditions
There is a case where the associative array inside PHP, if the following array data:
[PHP]
$array = Array (
Array (
' Name ' = ' Xiao ',
' Age ' = 3
),
Array (
' Name ' = ' Wang ',
' Age ' = 1
),
Array (
' Name ' = ' Chen ',
' Age ' = 2
)
);
We want to sort the array for the Age field, and PHP's own functions, no matter what sort, obviously don't meet our needs, so we can write a quick sort code ourselves, and quickly implement our requirements.
Note the situation
There is no pointer in PHP, so when we want to refer to the pass, we can not just like C code, so write quicksort (int *a, int begin, int end), but to use the PHP & operator, the address of the array is passed with the fast sorting function, This makes it possible to implement reference passing in PHP instead of value passing.
Quick Sort Code
[PHP]
Quicksortprocess ($array, 0, Count ($array)-1);
Print_r ($array);
/**
* Description: Get the location of the central point in the quick sort
*/
Function Quickpartition (& $array, $left, $right) {
1. Benchmark definition
$stand = $array [$left];
2. Scan from both ends to the middle 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. Get the Hub point location
$array [$left] = $stand;
return $left;
}
/**
* Description: Quick Sort main process function
*/
Function quicksortprocess (& $array, $begin, $end) {
1. Variable definition
$pivot = NULL; Central Point
if ($begin < $end) {
$pivot = Quickpartition ($array, $begin, $end);
Quicksortprocess ($array, $begin, $pivot-1);
Quicksortprocess ($array, $pivot + 1, $end);
}
}
I used this quick sort on the project, very happy, not in vain this October 1 vacation spent n days ac quick sort C code
http://www.bkjia.com/PHPjc/477946.html www.bkjia.com true http://www.bkjia.com/PHPjc/477946.html techarticle use environment and conditions there is a case where the associative array inside PHP, if the following array data: [php] $array = Array (Array (name = Xiao, age = 3), array (name = ...)