Wednesday, 18 December 2013

PHP sort a multi dimensional array with values


array_multisort — Sort multiple or multi-dimensional arrays

We have an array of rows, but array_multisort() requires an array of columns, so we use the below code to obtain the columns, then perform the sorting.

$resultData = array(0=>array('name'=>'test1', 'email'=> 'test1@test.com', 'phone'=>990000001),
                               1=>array('name'=>'test2', 'email'=> 'test2@test.com', 'phone'=>990000002),  
                               2=>array('name'=>'test3', 'email'=> 'test3@test.com', 'phone'=>990000003),
foreach ($resultData as $key => $row) {
     $mid[$key]  = $row['name'];
}
array_multisort($mid, SORT_DESC, $resultData);
 

No comments:

Post a Comment