sigkill-9 Posted March 2, 2009 Share Posted March 2, 2009 I have an array that contains movie information and am trying to figure out how to sort it via PHP but need help. Here is a sample of the array: ( [0] => Array ( [id] => 1 [title] => A.I. Artificial Intelligence [rating] => PG13 [description] => Sometime in the distant future, after the polar icecaps have melted.... ) [1] => Array ( [id] => 2 [title] => Shawshank Redemption [rating] => R [description] => Andy Dufresne, a mild mannered New England banker, is convicted of murdering his wife.... ) [2] => Array ( [id] => 3 [title] => Pulp Fiction [rating] => R [description] => An inside look at a memorable community of criminals.... ) ) I know how to ORDER BY in MySQL, but how would I sort this array in descending alphabetical order so that when displayed in an html select drop down box it is ordered by title? E.G.: A.I. Artificial Intelligence Pulp Fiction Shawshank Redemption Quote Link to comment https://forums.phpfreaks.com/topic/147542-solved-noob-need-help-sorting-a-multi-dimensional-array-alphabetically-descending/ Share on other sites More sharing options...
killah Posted March 2, 2009 Share Posted March 2, 2009 have you tried array_sort maybe? Quote Link to comment https://forums.phpfreaks.com/topic/147542-solved-noob-need-help-sorting-a-multi-dimensional-array-alphabetically-descending/#findComment-774507 Share on other sites More sharing options...
sigkill-9 Posted March 2, 2009 Author Share Posted March 2, 2009 yes, i've tried several varients but none seem to work how I need them to. arsort() sorts by ID, not by title array_multisort($title_array, SORT_DESC) sorts again by ID not by title like I need it to ksort($title_array, SORT_STRING); also does not give the required results It seems like it should be simple, but i'm going bald pulling out my hair over this thing lol. Isnt there a way to sort an array by one of it's values? something like: sigkill_sort($title_array, 'title'); Or do I have to make a function to do this? If so, how would I do that??? heres my select code. I attempted a ksort but it doesnt work. Doesnt order the titles alphabetically descending like I need: ksort($title_array['title']); foreach($title_array as $value) { $id = $value['id']; $title = $value['title']; $t = '<option value="' . $id . '"'; if ($value['id'] === $_POST['id']) $t .= ' SELECTED'; echo $t . '>'. $title . '</option>\n'; } $title_array contains the above mentioned array. Quote Link to comment https://forums.phpfreaks.com/topic/147542-solved-noob-need-help-sorting-a-multi-dimensional-array-alphabetically-descending/#findComment-774515 Share on other sites More sharing options...
Stephen68 Posted March 2, 2009 Share Posted March 2, 2009 Found this article hope it helps some http://www.informit.com/articles/article.aspx?p=31840&seqNum=6 Quote Link to comment https://forums.phpfreaks.com/topic/147542-solved-noob-need-help-sorting-a-multi-dimensional-array-alphabetically-descending/#findComment-774530 Share on other sites More sharing options...
sigkill-9 Posted March 2, 2009 Author Share Posted March 2, 2009 Thanks for the link. I read it and tried to apply it to my array, but it still doesnt seem to be sorting by title in the drop down list like I need it to. Theres go to be a way to do this. I'm new to PHP and dont know a whole lot, but i'm sure theres a way. Doesnt anyone know how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/147542-solved-noob-need-help-sorting-a-multi-dimensional-array-alphabetically-descending/#findComment-774928 Share on other sites More sharing options...
premiso Posted March 2, 2009 Share Posted March 2, 2009 <?php function scmp($a, $b, $key="title") { return strcmp($a[$key], $b[$key]); } $fruits[0]["title"] = "ZShould be last"; $fruits[1]["title"] = "AA Should be first"; $fruits[2]["title"] = "AB Should be second"; usort($fruits, "scmp"); while (list($key, $value) = each($fruits)) { echo "\$fruits[$key]: " . $value["title"] . "<br />"; } die(); ?> Never mind the name of the array ($fruits). That should get you what you want. Quote Link to comment https://forums.phpfreaks.com/topic/147542-solved-noob-need-help-sorting-a-multi-dimensional-array-alphabetically-descending/#findComment-774953 Share on other sites More sharing options...
sigkill-9 Posted March 2, 2009 Author Share Posted March 2, 2009 Thanks premiso. I adapted your example to my application and it works, it sorts the list, finally I have a question though, while waiting for a response to this post I searched elsewhere for a solution and I found another code that also works. The question is, which of these two codes would be better to use? Your code: function cmp($a, $b, $key="title") { return strcmp($a[$key], $b[$key]); } usort($title_array, "cmp"); while (list($key, $value) = each($title_array)) { echo "\$title_array[$key]: " . $value["title"] . "<br />"; } die(); Or the other code I found: function cmp($a, $b) { return strcmp($a["title"], $b["title"]); } usort($title_array, "cmp"); foreach($title_array as $value) { echo $value['title']."<br>"; } echo "<\pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/147542-solved-noob-need-help-sorting-a-multi-dimensional-array-alphabetically-descending/#findComment-774967 Share on other sites More sharing options...
premiso Posted March 2, 2009 Share Posted March 2, 2009 There really is not a difference. Whichever one you want to use should be fine. (Personally I would stick with the one you found). As mine is adapted for different functionality. Quote Link to comment https://forums.phpfreaks.com/topic/147542-solved-noob-need-help-sorting-a-multi-dimensional-array-alphabetically-descending/#findComment-774977 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.