PHP instructions on the use of array_multisort multidimensional array sort

  • 2020-03-31 21:28:24
  • OfStack

First, let's look at the simplest case. There are two arrays:
 
$arr1 = array(1,9,5); 
$arr2 = array(6,2,4); 
array_multisort($arr1,$arr2); 
print_r($arr1); //I get 1,5,9
print_r($arr2); //I get 6,4,2

I estimate that the values of the two arrays correspond throughout: 1 for 6,9 for 2,5 for 4.
Let's add one more array and see what happens:
 
$arr1 = array(1,9,5); 
$arr2 = array(6,2,4); 
$arr3 = array(3,7,8); 
array_multisort($arr1,$arr2,$arr3); 

So if you look at it, 1 goes all the way to 6 and 3, and the rest of it goes all the way to 3. This correspondence is what the manual calls "keeping the original key name association when sorting".
Alternatively, you can think of each array as a column of a database table. The corresponding 1,6,3 is one data row, 9,2,7 is another data row...
Array_multisort will first sort the first array (think of it as a column) and then the second array (column) if the first array (column) has the same value.
The following program can be used to test:
 
$arr1 = array(1,9,5,9); 
$arr2 = array(6,2,4,1); 
$arr3 = array(3,7,8,0); 
array_multisort($arr1,$arr2,$arr3); 

You can imagine the result of $arr3 here is (3,8,0,7).
Next, the parameters of array_multisort are explained. The arguments to this function are flexible. The simplest case is to take 1 or n arrays as arguments as shown above. It is important to note that the number of items in each array should be the same, otherwise warning will cause the sort to fail.
Like this array_multisort ($arr1, $arr2, $arr3); The default is that all arrays are in ascending order. If you want to descending order $arr2 and compare it as a string, you should write:
Array_multisort ($arr1, $arr2, SORT_DESC, SORT_STRING, $arr3);
Each array can be followed by a sort order flag or a sort type flag, or both. However, only one sort flag can appear after each array.
Details are as follows:
Sort order flag:

SORT_ASC - sort in ascending order (default)
SORT_DESC - sort in descending order

Sort type flag:

SORT_REGULAR - compare items as usual (default)
SORT_NUMERIC - compares items in numerical terms
SORT_STRING - compares items to strings

Finally, what the array_multisort actually does.
We usually have some multidimensional arrays to sort:
$guys = Array
(
[0] = > Array
(
[name] = > jake
[score] = > 80
[grade] = > a.
)
[1] = > Array
(
[name] = > jin
[score] = > 70
[grade] = > a.
)
[2] = > Array
(
[name] = > John
[score] = > 80
[grade] = > a.
)
[3] = > Array
(
[name] = > Ben
[score] = > 20
[grade] = > B
)
)
For example, we want to rank them in reverse order, and in ascending order if they are the same. At this point we need two more arrays in the order of $guys: $scores = array(80,70,80,20); $names = array (' jake ', 'jin', 'John', 'Ben'); Then array_multisort($scores, SORT_DESC, $names, $guys); Can we be a little bit more flexible? Every time we want to sort we have to make some extra array? In fact, in the helper_array class of qeephp, it has been well encapsulated, the following are its two methods, need to modify their own can be used:
 
/** 
*  Sorts the array according to the specified key  
* 
*  Usage:  
* @code php 
* $rows = array( 
* array('id' => 1, 'value' => '1-1', 'parent' => 1), 
* array('id' => 2, 'value' => '2-1', 'parent' => 1), 
* array('id' => 3, 'value' => '3-1', 'parent' => 1), 
* array('id' => 4, 'value' => '4-1', 'parent' => 2), 
* array('id' => 5, 'value' => '5-1', 'parent' => 2), 
* array('id' => 6, 'value' => '6-1', 'parent' => 3), 
* ); 
* 
* $rows = Helper_Array::sortByCol($rows, 'id', SORT_DESC); 
* dump($rows); 
* //The output result is:
* // array( 
* // array('id' => 6, 'value' => '6-1', 'parent' => 3), 
* // array('id' => 5, 'value' => '5-1', 'parent' => 2), 
* // array('id' => 4, 'value' => '4-1', 'parent' => 2), 
* // array('id' => 3, 'value' => '3-1', 'parent' => 1), 
* // array('id' => 2, 'value' => '2-1', 'parent' => 1), 
* // array('id' => 1, 'value' => '1-1', 'parent' => 1), 
* // ) 
* @endcode 
* 
* @param array $array  The array to sort  
* @param string $keyname  The sort of key  
* @param int $dir  Sort direction  
* 
* @return array  Sorted array  
*/ 
static function sortByCol($array, $keyname, $dir = SORT_ASC) 
{ 
return self::sortByMultiCols($array, array($keyname => $dir)); 
}  
static function sortByMultiCols($rowset, $args) 
{ 
$sortArray = array(); 
$sortRule = ''; 
foreach ($args as $sortField => $sortDir) 
{ 
foreach ($rowset as $offset => $row) 
{ 
$sortArray[$sortField][$offset] = $row[$sortField]; 
} 
$sortRule .= '$sortArray['' . $sortField . ''], ' . $sortDir . ', '; 
} 
if (empty($sortArray) || empty($sortRule)) { return $rowset; } 
eval('array_multisort(' . $sortRule . '$rowset);'); 
return $rowset; 
} 


Related articles: