Malthe Milthers Malthe Milthers - 7 months ago 27
PHP Question

php array_multisort how to but the SORT_ASC and SORT_DESC into variables

Hi im interested to let my query tell my web app how to sort an array.

Everything is working fine, but i can't figure out a clever way to have the URL (page.php?order=asc) grab that query with:

$order = $_GET['order'];


and put it into:

array_multisort($sort['name'], SORT_ASC, $array);


this doesnt seem to work:

$test = SORT_ASC;
array_multisort($sort['name'], $test, $array);


Had a hard time googling for this, so hopefully some smart brain here can help me out :)
thank you

Answer

Your second code example using $test should work, I use that all the time. Here is how to use the asc from $_GET['order']:

$order = constant('SORT_' . strtoupper($_GET['order']));
array_multisort($sort['name'], $order, $array);