benphp Posted March 2, 2009 Share Posted March 2, 2009 This is probably simple for someone. I want to sort an array but maintain the array index (0, 1, 2, etc). Here's my example: <?php $testy = array (); $testy[] = "zebra"; $testy[] = "alligator"; $testy[] = "panda"; $testy[] = "kangaroo"; asort($testy); for($K = 0; $K < sizeof($testy); $K++) { $id = $K; print "ID = ".$K.";"; print "Animal = ". $testy[$K]; print "<br />"; } ?> This sorts the array, but my index gets jacked. It displays: ID = 0;Animal = zebra ID = 1;Animal = alligator ID = 2;Animal = panda ID = 3;Animal = kangaroo I want it to display: ID = 3;Animal = zebra ID = 0;Animal = alligator ID = 3;Animal = panda ID = 2;Animal = kangaroo Help! Link to comment https://forums.phpfreaks.com/topic/147640-help-sorting-array/ Share on other sites More sharing options...
rhodesa Posted March 2, 2009 Share Posted March 2, 2009 http://us3.php.net/manual/en/function.asort.php edit: Tip for next time. If you know you are close, go to the PHP Doc for that function (in this case sort()), scroll down to See Also, and there is a list of similar functions and what the do. On the doc for sort, asort() is listed in the See Also as asort() - Sort an array and maintain index association Link to comment https://forums.phpfreaks.com/topic/147640-help-sorting-array/#findComment-775051 Share on other sites More sharing options...
benphp Posted March 2, 2009 Author Share Posted March 2, 2009 I've been all through that - usort, sort, asort, - none of them do what I'm trying to do. Link to comment https://forums.phpfreaks.com/topic/147640-help-sorting-array/#findComment-775061 Share on other sites More sharing options...
corbin Posted March 2, 2009 Share Posted March 2, 2009 The desired data has no correlation to the data you showed us in the example code. Link to comment https://forums.phpfreaks.com/topic/147640-help-sorting-array/#findComment-775065 Share on other sites More sharing options...
benphp Posted March 2, 2009 Author Share Posted March 2, 2009 You're right sorry. I want it to display: ID = 1;Animal = alligator ID = 3;Animal = kangaroo ID = 2;Animal = panda ID = 0;Animal = zebra That is, it sorts on the values but retains the indexes. Link to comment https://forums.phpfreaks.com/topic/147640-help-sorting-array/#findComment-775072 Share on other sites More sharing options...
corbin Posted March 2, 2009 Share Posted March 2, 2009 Your for loop is what is messing things up. You want the keys to remain in order, but then you access them sequentially. Doesn't make much sense. You could just do a foreach loop: foreach($testy as $key => $val) { //key is the original key } Link to comment https://forums.phpfreaks.com/topic/147640-help-sorting-array/#findComment-775076 Share on other sites More sharing options...
benphp Posted March 2, 2009 Author Share Posted March 2, 2009 That's it. Thanks! What was I thinking. Link to comment https://forums.phpfreaks.com/topic/147640-help-sorting-array/#findComment-775083 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.