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? Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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(). Quote Link to comment 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 Quote Link to comment 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.