php Array Sort usort uksort and sort Function Usage

  • 2021-08-03 09:46:33
  • OfStack

This article illustrates the use of php array sorting usort, uksort, and sort functions. Share it for your reference. The specific usage analysis is as follows:

Sort arrays: The usort () function uses a user-defined function to sort arrays, with the following example code:

function cmp($a, $b)        // User-defined callback function  
{
  if($a==$b)         // If two parameters are equal
  {
    return 0;         // Return 0
  }
  return($a>$b)?-1:1;       // If the 1 Parameters greater than the first 2 Return 1 Otherwise -1
}
$a=array(3,2,5,6,1);        // Definition 1 Array of numbers
usort ($a,"cmp");        // Sorting Arrays Using Custom Functions
foreach($a as $key=>$value)      // Loop output sorted key-value pairs
{
  echo "$key:$valuen";
}

Note: If two elements compare the same, their order in the sorted array is undefined. Until php 4.0. 6, the user-defined function will retain the original order of these elements, but due to the introduction of a new sorting algorithm in 4.1. 0, the result will not be the same, because there is no effective solution for this.

Sort the array key names uksort (array, sorttype) with the following example code:

function cmp($a, $b)        // User-defined callback function  
{
  if($a==$b)         // If two parameters are equal
  {
    return 0;         // Return 0
  }
  return($a>$b)?-1:1;       // If the 1 Parameters greater than the first 2 Return 1 Otherwise -1
}
$a=array(4=>"four",3 =>"three",20 =>"twenty",10=>"ten"); // Definition 1 Array of numbers
uksort ($a,"cmp");        // Sorting Array Key Names Using Custom Functions
foreach($a as $key=>$value)      // Loop output sorted key-value pairs
{
  echo "$key:$valuen";
}

The uksort () function uses a user-defined comparison function to sort the array by key name, preserving the index relationship, and returns true if successful, or false if not.

This function should be used if the array to be sorted needs to be sorted by an unusual standard. The custom function should take two arguments, which will be filled with a pair of key names in the array. The comparison function must return an integer less than zero, equal to zero, or greater than zero if the first argument is less than, equal to, or greater than the second argument, respectively.

The sort () function sorts the values of a given array in ascending order.

Note: This function gives a new key name to the cell in the array, and the original key name will be deleted. If it is successful, it will return true, otherwise it will return false. The example code is as follows:

$fruits=array("lemon","orange","banana","apple");    // Definition 1 Array of numbers  
sort($fruits);           // Sort an array
foreach($fruits as $key=>$val)        // Loop output array sorted key-value pairs
{
  echo "$key=$valn";         // Output key-value pair
}

I hope this article is helpful to everyone's PHP programming.


Related articles: