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!!