moon 111 Posted April 7, 2008 Share Posted April 7, 2008 I have a 3 arrays $arr1, $arr2 and $arr3. I want to sort all of them by $arr3 so that if $arr3 has the values 5, 9, 7 it'll become 5, 7, 9 and the same thing will happen to the other two arrays. How can I do this? Link to comment https://forums.phpfreaks.com/topic/99949-sorting-arrays/ Share on other sites More sharing options...
Cep Posted April 7, 2008 Share Posted April 7, 2008 Read this http://uk2.php.net/manual/en/function.sort.php Link to comment https://forums.phpfreaks.com/topic/99949-sorting-arrays/#findComment-511070 Share on other sites More sharing options...
moon 111 Posted April 7, 2008 Author Share Posted April 7, 2008 No. That won't help me. I already tried that. Example: $title = array('Hello!', 'Title #2', 'A title!'); $description = array('message', 'blah', 'text'); If I sort $title and $description seperately it will only mess it up. I want to sort $title and then apply the same thing to $description without mixing up the values. What I want the arrays to look like after the sort is: $title = array('A title!', 'Title #2', 'Hello!'); $description = array('text', 'blah', 'message'); Get it? Link to comment https://forums.phpfreaks.com/topic/99949-sorting-arrays/#findComment-511074 Share on other sites More sharing options...
Cep Posted April 7, 2008 Share Posted April 7, 2008 I don't understand what type of sort your performing from your example, it is neither alphabetical or numerical. If your trying to do a custom sort you need usort(). Link to comment https://forums.phpfreaks.com/topic/99949-sorting-arrays/#findComment-511078 Share on other sites More sharing options...
GingerRobot Posted April 7, 2008 Share Posted April 7, 2008 You can stick them in a multidimensional array and use a user defined sort function. <?php $title = array('Hello!', 'Title #2', 'A title!'); $description = array('message', 'blah', 'text'); $array= array(); for($x=0;$x<count($title);$x++){ $array[$x]['title'] = $title[$x]; $array[$x]['description'] = $description[$x]; } function mysort($a,$b){ return strcmp($a['title'],$b['title']); } echo '<pre>'.print_r($array,1).'</pre>'; usort($array,'mysort'); echo '<pre>'.print_r($array,1).'</pre>'; ?> The returned array might not quite be what you were expecting, but it may actually be more useful. See: http://www.php.net/usort Link to comment https://forums.phpfreaks.com/topic/99949-sorting-arrays/#findComment-511081 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.