An example of PHP algorithm for sorting two dimensional arrays according to specified fields

  • 2021-12-09 08:20:05
  • OfStack

This article describes the example of PHP 2-dimensional array according to the specified field sorting algorithm. Share it for your reference, as follows:

Encountered a problem: Use the two arrays that come with php array_merge() After merging functions, you want to sort the new array according to the 'post_time' field that is common to both arrays

Solution: By consulting the official manual, I learned that there are array_multisort() This function sorts multiple arrays or multidimensional arrays and returns the sorted array where the string key name will be retained but the numeric key name will be re-indexed starting at 0 and incremented by 1.

This function is encapsulated below for easy calling:


/**
 * 2 The array of dimensions is sorted by the specified field 
 * @params array $array  Array to be sorted 
 * @params string $field  Sorted fields 
 * @params string $sort  Sort order flag  SORT_DESC  Descending order; SORT_ASC  Ascending order 
 */
function arraySequence($array, $field, $sort = 'SORT_DESC') {
 $arrSort = array();
 foreach ($array as $uniqid => $row) {
  foreach ($row as $key => $value) {
   $arrSort[$key][$uniqid] = $value;
  }
 }
 array_multisort($arrSort[$field], constant($sort), $array);
 return $array;
}
// Test: 
$arrDemo = array(
array('name'=>'Jack','age'=>'22'),
array('name'=>'Tom','age'=>'24'),
array('name'=>'Green','age'=>'21'),
array('name'=>'Ben','age'=>'23'),);
$arrDemo = arraySequence($arrDemo,'age');
print_r($arrDemo);

Run results:

Array
(
[0] = > Array
(
[name] = > Tom
[age] = > 24
)

[1] = > Array
(
[name] = > Ben
[age] = > 23
)

[2] = > Array
(
[name] = > Jack
[age] = > 22
)

[3] = > Array
(
[name] = > Green
[age] = > 21
)

)

Add: Sort by specified multiple fields


/**
 * 2 An array of dimensions is sorted by a specified number of fields 
 *
 *  Invoke the example: sortArrByManyField($arr,'id',SORT_ASC,'age',SORT_DESC);
 */
function sortArrByManyField(){
 $args = func_get_args();
 if(empty($args)){
  return null;
 }
 $arr = array_shift($args);
 if(!is_array($arr)){
  throw new Exception(" No. 1 1 Parameters should be arrays ");
 }
 foreach($args as $key => $field){
  if(is_string($field)){
   $temp = array();
   foreach($arr as $index=> $val){
    $temp[$index] = $val[$field];
   }
   $args[$key] = $temp;
  }
 }
 $args[] = &$arr;// Reference value 
 call_user_func_array('array_multisort',$args);
 return array_pop($args);
}
// Test: 
$arrDemo = array(
array('name'=>'Jack','age'=>'22'),
array('name'=>'Tom','age'=>'24'),
array('name'=>'Green','age'=>'21'),
array('name'=>'Ben','age'=>'23'),);
$arrDemo = sortArrByManyField($arrDemo,'age');
print_r($arrDemo);

Run results:

Array
(
[0] = > Array
(
[name] = > Green
[age] = > 21
)

[1] = > Array
(
[name] = > Jack
[age] = > 22
)

[2] = > Array
(
[name] = > Ben
[age] = > 23
)

[3] = > Array
(
[name] = > Tom
[age] = > 24
)

)

PS: Here we recommend another demonstration tool about sorting for your reference:

Online animation demonstrates insert/select/bubble/merge/hill/quick sort algorithm process tool:
http://tools.ofstack.com/aideddesign/paixu_ys

For more readers interested in PHP related contents, please check the special topics of this site: "PHP Array (Array) Operation Skills Encyclopedia", "php Sorting Algorithm Summary", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "php String (string) Usage Summary" and "PHP Common Traversal Algorithms and Skills Summary"

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


Related articles: