TFT2012 Posted June 6, 2012 Share Posted June 6, 2012 Hi all, I have a question regarding array_multisort(). For instance, I have one array as follow: $arr = array( array("PSize" => 216597, "DSize" => 1, "CFSize" => 172504 ), array("PSize" => 106597, "DSize" => 10, "CFSize" => 61311 ), array("PSize" => 166597, "DSize" => 5, "CFSize" => 261311 ), array("PSize" => 396597, "DSize" => 8, "CFSize" => 101311 ) ); In HTML, I have multiple selection boxes to select columns and types to sort. Each column will have one selection box like this. <select id='selectorId'> <option value='none'>None</option> <option value='SORT_ASC'>Ascending</option> <option value='SORT_DESC'>Descending</option> </select> When submit, I will return the string like "~column_1|sorting_type_1~column_2|sorting_type_2~...." back to AJax. If user selects "none", that means NO SORT to this column. Then I want: For example, if "PSize" is set to "none", the other two columns are set to "SORT_ASC", I can do foreach( $arr as $key => $row ) { //Only get the columns user want to sort. $sort_dsize[] = $row["DSize"]; $sort_cfsize[] = $row["CFSize"]; } array_multisort($sort_dsize, SORT_ASC, $sort_cfsize, SORT_ASC, $arr); If user only want to sort "PSize", then foreach( $arr as $key => $row ) { //Only get the columns user want to sort. $sort_psize[] = $row["PSize"]; } array_multisort($sort_psize, SORT_ASC, $arr); How can I do this dynamically? Not to use swtich, case or if/else. Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/263767-array-multisort-question/ Share on other sites More sharing options...
Skewled Posted June 6, 2012 Share Posted June 6, 2012 https://www.dougv.com/2009/06/13/sorting-your-mysql-results-set-in-php-using-jquery-and-a-more-traditional-approach/ See the JQuery portion. Quote Link to comment https://forums.phpfreaks.com/topic/263767-array-multisort-question/#findComment-1351686 Share on other sites More sharing options...
Barand Posted June 8, 2012 Share Posted June 8, 2012 Custom sort on multiple columns. Note that in your params I have used 0 for none, 1 for ASC and -1 for DESC sort on that column (easy then to just mutiply result of comparison) <?php $paramStr = "Psize|0~DSize|-1~CFSize|1"; // none = 0, ASC = 1, DESC = -1 $parr = explode('~', $paramStr); foreach ($parr as $p) { $params[] = explode('|', $p); } function mysort ($a, $b) { global $params; $x1 = ($a[$params[0][0]] - $b[$params[0][0]]) * $params[0][1]; if ($x1==0) { $x2 = ($a[$params[1][0]] - $b[$params[1][0]]) * $params[1][1]; if ($x2==0) { return ($a[$params[2][0]] - $b[$params[2][0]]) * $params[2][1]; } else return $x2; } else return $x1; } $arr = array( array("PSize" => 216597, "DSize" => 1, "CFSize" => 172504 ), array("PSize" => 106597, "DSize" => 10, "CFSize" => 61311 ), array("PSize" => 166597, "DSize" => 5, "CFSize" => 261311 ), array("PSize" => 396597, "DSize" => 5, "CFSize" => 101311 ) ); uasort($arr, 'mysort'); echo '<pre>'.print_r($arr, 1).'</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/263767-array-multisort-question/#findComment-1352102 Share on other sites More sharing options...
TFT2012 Posted June 27, 2012 Author Share Posted June 27, 2012 Thanks for the solution. Custom sort on multiple columns. Note that in your params I have used 0 for none, 1 for ASC and -1 for DESC sort on that column (easy then to just mutiply result of comparison) <?php $paramStr = "Psize|0~DSize|-1~CFSize|1"; // none = 0, ASC = 1, DESC = -1 $parr = explode('~', $paramStr); foreach ($parr as $p) { $params[] = explode('|', $p); } function mysort ($a, $b) { global $params; $x1 = ($a[$params[0][0]] - $b[$params[0][0]]) * $params[0][1]; if ($x1==0) { $x2 = ($a[$params[1][0]] - $b[$params[1][0]]) * $params[1][1]; if ($x2==0) { return ($a[$params[2][0]] - $b[$params[2][0]]) * $params[2][1]; } else return $x2; } else return $x1; } $arr = array( array("PSize" => 216597, "DSize" => 1, "CFSize" => 172504 ), array("PSize" => 106597, "DSize" => 10, "CFSize" => 61311 ), array("PSize" => 166597, "DSize" => 5, "CFSize" => 261311 ), array("PSize" => 396597, "DSize" => 5, "CFSize" => 101311 ) ); uasort($arr, 'mysort'); echo '<pre>'.print_r($arr, 1).'</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/263767-array-multisort-question/#findComment-1357486 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.