therealwesfoster Posted February 18, 2008 Share Posted February 18, 2008 I have an array like so: $pics = array ( array("name" => "wes", "number" => 14), array("name" => "jared", "number" => 97), array("name" => "amber", "number" => 2) ); So $pics[0]['name'] will equal wes. Well, I'm wanting to use these in a FOR loop, but I'm wanting to first sort them by their number and have it re-arrange the indexes. So once i sort them, I want it to be in the order 0 = amber,1 = wes,2 = jared instead of the current 0 = wes,1 = jared,2 = amber How would I do this? btw, I've read every sorting method in the php manual.. but for some reason I'm still at a loss.. so please provide an example. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/91708-sort-this-multi-array/ Share on other sites More sharing options...
sasa Posted February 18, 2008 Share Posted February 18, 2008 try <?php $pics = array ( array("name" => "wes", "number" => 14), array("name" => "jared", "number" => 97), array("name" => "amber", "number" => 2) ); function my_comp($a,$b){ if($a['number']<$b['number']) return -1; if($a['number']>$b['number']) return 1; return 0; } usort($pics,'my_comp'); print_r($pics); ?> Quote Link to comment https://forums.phpfreaks.com/topic/91708-sort-this-multi-array/#findComment-469723 Share on other sites More sharing options...
therealwesfoster Posted February 18, 2008 Author Share Posted February 18, 2008 Awesome.. it makes sense now Thanks Quote Link to comment https://forums.phpfreaks.com/topic/91708-sort-this-multi-array/#findComment-469806 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.