In this tutorial learn how to sort Array by subArray value, we using array_multisort() method and array_map() method to sort multidimensional array.
in this function pass the multidimensional and set the return value of SubArray element/value to Sorting an Array,
can be sort array by ascending using SORT_ASC or Descending using SORT_DESC.
here just replaced $array with <array_name> and SubArray value with <sub_array_value> in this function only
Complete Tutorial sort array by SubArray value
in this function pass the multidimensional and set the return value of SubArray element/value to Sorting an Array,
can be sort array by ascending using SORT_ASC or Descending using SORT_DESC.
here just replaced $array with <array_name> and SubArray value with <sub_array_value> in this function only
array_multisort(array_map(function($element) {
return $element['<sub_array_value>'];
}, <array_name>), SORT_ASC, $array);
Complete Tutorial sort array by SubArray value
<?php
$array = Array
(
0 => Array
(
'user_id' => 10,
'id' => 1,
'Number' => 3,
'Active' => 1,
'Updated' => '2010-03-17 15:44:12',
),
1 => Array
(
'user_id' => 9,
'id' => 2,
'Number' => 2,
'Active' => 1,
'Updated' => '2010-03-17 15:44:12',
),
2 => Array
(
'user_id' => 8,
'id' => 3,
'Number' => 1,
'Active' => 1,
'Updated' => '2010-03-17 15:44:12',
)
);
array_multisort(array_map(function($element) {
return $element['user_id'];
}, $array), SORT_ASC, $array);
print_r($array);
OUTPUT of Above Tutorial : demoArray ( [0] => Array ( [user_id] => 8 [id] => 3 [Number] => 1 [Active] => 1 [Updated] => 2010-03-17 15:44:12 ) [1] => Array ( [user_id] => 9 [id] => 2 [Number] => 2 [Active] => 1 [Updated] => 2010-03-17 15:44:12 ) [2] => Array ( [user_id] => 10 [id] => 1 [Number] => 3 [Active] => 1 [Updated] => 2010-03-17 15:44:12 ) )
No comments:
Post a Comment