SchweppesAle Posted May 18, 2009 Share Posted May 18, 2009 hi, I'd like to sort a multidimensional array by the values of each element. I'm having some trouble though. $list = $_POST["list"]; $count = count($list); $max = max($list); include("Connect.php"); DBConnect(); mysql_select_db("account6_jo152"); $result = mysql_query("Select * FROM jos_content ORDER BY created DESC") or die(mysql_error()); while($row = mysql_fetch_array($result)) { $id[] = $row['id']; } $maxID = max($id); array_multisort($list, SORT_NUMERIC); for ($i = 0; $i <= $count; $i++) { for ($t = 0; $t <= $maxID; $t++) { if(($list[$i][$t]) != 0) { $itemList[] = $t; } } } The idea is to sort the array out then remove all elements who's value == 0 before placing them into a seperate array. Thanks in advance for your help. Link to comment https://forums.phpfreaks.com/topic/158550-solved-how-to-sort-multi-dimensional-array-by-value/ Share on other sites More sharing options...
Masna Posted May 18, 2009 Share Posted May 18, 2009 array_multisort() should do the trick, but I see that you have that in there already. Can you post what the $list starts as, and what it ends as (after applying array_multisort())? Link to comment https://forums.phpfreaks.com/topic/158550-solved-how-to-sort-multi-dimensional-array-by-value/#findComment-836217 Share on other sites More sharing options...
SchweppesAle Posted May 18, 2009 Author Share Posted May 18, 2009 Before/After using multisort() the output displayed by the action page seems to sort the array by ID rather than the value associated with all elements of the array. Here's the code I used in generating it. echo Generate_List(); function Generate_List() { $result = mysql_query("Select * FROM jos_content ORDER BY created DESC") or die(mysql_error()); $output = '<div style = "width:750px; margin-left:auto; margin-right:auto"><table border = "1" width = "750">'; $output .='<tr><td>Title</td><td>Created</td><td>Order</td></tr><form action = "generate_newsletter.php" method = "post">'; $counter = 1; while($row = mysql_fetch_array($result)) { $id = $row['id']; $output .= '<tr><td>'; $output .= $row['title']; $output .= '</td><td>'.$row['created']; $output .= '</td><td><input type = "text" maxlength = "3" size = "5" name = "list['.$counter.']['.$id.']" value = "0" />'; $output .= '</td></tr>'; $counter++; } $output .= '<input type = "submit" />'; $output .= '</form></td></tr></table></div>'; return $output; } since it's in DESC order the newest content items are displayed at the top; usually these entries have the highest ID attribute. The array itself should consist of about 127(position grows to 127) entries, most of them 0. Link to comment https://forums.phpfreaks.com/topic/158550-solved-how-to-sort-multi-dimensional-array-by-value/#findComment-836225 Share on other sites More sharing options...
SchweppesAle Posted May 19, 2009 Author Share Posted May 19, 2009 the solution was simple. arsort($list); Link to comment https://forums.phpfreaks.com/topic/158550-solved-how-to-sort-multi-dimensional-array-by-value/#findComment-836859 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.