TomT Posted June 25, 2012 Share Posted June 25, 2012 Hi My array is made using : $group[] = array("ctime"=>$ctime,"name"=>$name that results in : [4]=> array(2) { ["ctime"]=> int(1334597042) ["name"]=> string(7) "John.dat" } [3]=> array(2) { ["ctime"]=> int(1334935095) ["name"]=> string(4) "mark" } [2]=> array(2) { ["ctime"]=> int(1340634768) ["name"]=> string(7) "lol.txt" } [1]=> array(2) { ["ctime"]=> int(1334597041) ["name"]=> string( "dave.txt" } [0]=> array(2) { ["ctime"]=> int(1335193200) ["name"]=> string(4) "paul" } How would I sort this array ascending based on the ctime values ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/264763-sorting-array/ Share on other sites More sharing options...
nibbrwebdevelopment Posted June 25, 2012 Share Posted June 25, 2012 $_group = array(); foreach ( $group as $g ) { $_group[] = sort ( $g, SORT_NUMERIC ); } $group = $_group; Not tested this. Quote Link to comment https://forums.phpfreaks.com/topic/264763-sorting-array/#findComment-1356928 Share on other sites More sharing options...
xyph Posted June 25, 2012 Share Posted June 25, 2012 Here's an actual working solution <?php $array = array( array( 'id'=>24, 'name'=>'foo' ), array( 'id'=>13, 'name'=>'bar' ), array( 'id'=>18, 'name'=>'baz' ), ); function custom_sort($a, $b) { return $a['id'] - $b['id']; } usort($array, 'custom_sort'); var_dump($array); ?> returns array 0 => array 'id' => int 13 'name' => string 'bar' (length=3) 1 => array 'id' => int 18 'name' => string 'baz' (length=3) 2 => array 'id' => int 24 'name' => string 'foo' (length=3) Quote Link to comment https://forums.phpfreaks.com/topic/264763-sorting-array/#findComment-1356934 Share on other sites More sharing options...
TomT Posted June 26, 2012 Author Share Posted June 26, 2012 Thanks using usort works perfectly One last question, how easy is it to do the same but sort in reverse order ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/264763-sorting-array/#findComment-1357014 Share on other sites More sharing options...
TomT Posted June 26, 2012 Author Share Posted June 26, 2012 Doing this seems to have worked: function custom_sort($a, $b) { return $b['id'] - $a['id']; } Quote Link to comment https://forums.phpfreaks.com/topic/264763-sorting-array/#findComment-1357018 Share on other sites More sharing options...
Barand Posted June 26, 2012 Share Posted June 26, 2012 Yes, reversing $a and $b gives descending sort. AFAIK just using sort() on a 2D array will sort on the the first element eg <?php $a = array( array( 'x' => 20, 'y' => 'ab'), array( 'x' => 30, 'y' => 'cd'), array( 'x' => 10, 'y' => 'ef') ); sort($a); echo '<pre>'.print_r($a, 1).'</pre>'; ?> --> Array ( [0] => Array ( [x] => 10 [y] => ef ) [1] => Array ( [x] => 20 [y] => ab ) [2] => Array ( [x] => 30 [y] => cd ) ) Quote Link to comment https://forums.phpfreaks.com/topic/264763-sorting-array/#findComment-1357023 Share on other sites More sharing options...
TomT Posted June 26, 2012 Author Share Posted June 26, 2012 Thanks. I'll test that and see what it does for me Quote Link to comment https://forums.phpfreaks.com/topic/264763-sorting-array/#findComment-1357031 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.