This article mainly introduces php's simple method for sorting multi-dimensional arrays, involving php array traversal and array_multisort functions, for more information about how to sort multidimensional arrays in php, see the following example. We will share this with you for your reference. The details are as follows:
Before doing a function, you must put the data in a two-dimensional array and sort the data, and then search for a solution on the Internet,
The array_multisort function is used to sort multiple arrays or multi-dimensional arrays.
First, let's look at an instance.
array('one'=>34,'two'=>'d'),1=>array('one'=>45,'two'=>'e'),2=>array('one'=>47,'two'=>'h'),3=>array('one'=>12,'two'=>'c'),4=>array('one'=>15,'two'=>'w'),5=>array('one'=>85,'two'=>'r'),);foreach($data as $val){$key_arrays[]=$val['one'];}array_multisort($key_arrays,SORT_ASC,SORT_NUMERIC,$data);var_dump($data);
Output result: the key values are sorted as follows:
array 0 => array 'one' => int 12 'two' => string 'c' (length=1) 1 => array 'one' => int 15 'two' => string 'w' (length=1) 2 => array 'one' => int 34 'two' => string 'd' (length=1) 3 => array 'one' => int 45 'two' => string 'e' (length=1) 4 => array 'one' => int 47 'two' => string 'h' (length=1) 5 => array 'one' => int 85 'two' => string 'r' (length=1)
Php multi-dimensional array sorting
Which is encapsulated as a function for ease of use
function my_array_multisort($data,$sort_order_field,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC){foreach($data as $val){$key_arrays[]=$val[$sort_order_field];}array_multisort($key_arrays,SORT_ASC,SORT_NUMERIC,$data);rturn $data;}
I hope this article will help you with PHP programming.
For more articles about how php simply sorts multi-dimensional arrays, refer to PHP Chinese network!