PHP array sorting sort, asort, and ksort usage, asortksort
Share the sort, asort, and ksort usages of the PHP array, which is a simple example of the use of sort, asort, and Ksort, with comments detailing them.
The use of sort, asort, and Ksort in the ordering of PHP arrays.
PHP$arrarray(' d ' = ' sdf ', ' r ' = ' sdf ', ' a ' = ' eee '); // sort ($arr);//The value of the array is re-//asort, the previous key value is deleted, and the index array is changed to the indexed arrays ($arr);//The array is re-sorted by value, and the index relationship is maintained, and the indexed and associative arrays apply Ksort ($arr// arrays are re-sorted by key value and maintain index relationships, both indexed and associative arrays apply//correspond to reverse order and Rsort Arsort krsort//use function compare Usort Uksort Uasort The second parameter for the comparison function needs to return a value equal to less than 0 when the first parameter equals less than the second argument, and the floating-point number will only take the integer part //www.jbxue.comprint_r( $arr);? >
In this example, only ksort usage is demonstrated, and you can test the results of sort and asort in order to deepen your impressions and grasp them firmly.
PHP uses function Asort and ksort to sort array elements
Do not know how you write, is less a planet1, but do not have it, you can use this 2, there is the array that block with a comma, do not use semicolons, the end of the statement with a semicolon. Here's my change.
$planet 2=array (
' X ' = ' Earth ',
' Y ' = ' Venus ',
' Z ' = ' Mars ',
' A ' = ' Jupiter ',
' B ' = ' Saturn ',
);
Asort ($planet 2);
Echo ' uses the function Asort to sort the array elements: ';
Echo ' ';
foreach ($planet 2 as $key = $value)
{
Echo ' planet2['. $key. ' = '. $value;
Echo ' ;
Echo ' ;
}
Echo ' ;
Echo ' uses the function Ksort to sort the array elements: ';
Echo ' ;
Ksort ($planet 2);
foreach ($planet 2 as $key = $value)
{
Echo ' planet2['. $key. ' = '. $value;
Echo ' ;
Echo ' ;
}
?>
PHP reverse order and positive order arrangement
In fact, look at the PHP manual is still helpful, LZ said that the positive sequence and reverse is used in what? If it is used directly on the array:
Sort arrays by Key name: Ksort ($array)
Reverse order of array by key name: Krsort ($array)
Sort an array and keep the index relationship: Asort ($array)
Reverse-Sort an array and keep the index relationship: Arsort ($array)
Reverse sequence of arrays: Rsort
Sort by array: sort
http://www.bkjia.com/PHPjc/876330.html www.bkjia.com true http://www.bkjia.com/PHPjc/876330.html techarticle php Array Sort, asort and ksort usage, Asortksort share the sort, asort, and ksort usage of the PHP array, the example is a simple demonstration of the use of sort, asort and ksort, with annotations to ...