We often use arrays for sorting in the array operations of PHP, which is about sorting associative arrays
You need to use the function Array_multisort.
Array_multisort (Array_column ($arr, ' weight '), Sort_numeric, SORT_ASC, $arr); convert key Column ' weight ' to numbers in ascending order for associative array $arr
Sort_numeric //Convert to digital sorting
Sort_string //Convert to text sorting
SORT_ASC //Ascending
Sort_desc //Descending
Example:
Original Associative array:
$data [] = Array (' volume ' = ' id100343 ', ' weight ' = ' 4 ');
$data [] = Array (' volume ' = ' id100212 ', ' weight ' = ' 1 ');
$data [] = Array (' volume ' = ' id104104 ', 'weight ' = ' 10 ');
Var_dump ($data);
Sort by weight (digital sort_numeric):
Output Result:
Array (3) { [0]=> Array (2) { ["Volume"]=> string (8) "id100212" [" Weight "]=> string (1)" 1 " } [1]=> Array (2) { [" volume "]=> string (8)" id100343 " ["Weight"]=> string (1) "4" } [2]=> Array (2) { ["volume"]=> string (8) " id104104 " [" Weight "]=> string (2)" Ten " }}
Sort by weight (text mode sort_string):
Array (3) { [0]=> Array (2) { ["Volume"]=> string (8) "id100212" [" Weight "]=> string (1)" 1 " } [1]=> Array (2) { [" volume "]=> string (8)" id104104 " ["Weight"]=> string (2) "Ten" } [2]=> Array (2) { ["volume"]=> string (8) " id100343 " [" Weight "]=> string (1)" 4 " }}
It is important to note that 10 is the difference between numbers and text.
Array_multisort to sort associative arrays PHP