php - Sort Multi-dimensional Array by Value -
possible duplicate:
how sort multidimensional array in php
how can sort array value of "order" key? though values sequential, not be.
array (     [0] => array         (             [hashtag] => a7e87329b5eab8578f4f1098a152d6f4             [title] => flower             [order] => 3         )      [1] => array         (             [hashtag] => b24ce0cd392a5b0b8dedc66c25213594             [title] => free             [order] => 2         )      [2] => array         (             [hashtag] => e7d31fc0602fb2ede144d18cdffd816b             [title] => ready             [order] => 1         ) ) 
try usort: if still on php 5.2 or earlier, you'll have define sorting function first:
function sortbyorder($a, $b) {     return $a['order'] - $b['order']; }  usort($myarray, 'sortbyorder'); starting in php 5.3, can use anonymous function:
usort($myarray, function($a, $b) {     return $a['order'] - $b['order']; }); and php 7 can use "spaceship operator":
usort($myarray, function($a, $b) {     return $a['order'] <=> $b['order']; }); to extend multi-dimensional sorting, reference second/third sorting elements if first 0 - best explained below. can use sorting on sub-elements.
usort($myarray, function($a, $b) {     $retval = $a['order'] <=> $b['order'];     if ($retval == 0) {         $retval = $a['suborder'] <=> $b['suborder'];         if ($retval == 0) {             $retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder'];         }     }     return $retval; }); if need retain key associations, use uasort() - see comparison of array sorting functions in manual
wiki
Comments
Post a Comment