JS gevorderde |
|
<?
function sorteer($arr,$waarop,$asc)
{
$sort = "\$arr[\"".$waarop."\"],".(($asc=="desc")?"SORT_DESC":"SORT_ASC");
foreach($arr as $key => $value)
{
if($key != $waarop)
{
$sort .= ",\$arr[\"".$key."\"]";
}
}
eval("array_multisort(".$sort.");");
return $arr;
}
?>
<? function sorteer($arr,$waarop,$asc) { $sort = "\$arr[\"".$waarop."\"],".(($asc=="desc")?"SORT_DESC":"SORT_ASC"); foreach($arr as $key => $value) { if($key != $waarop) { $sort .= ",\$arr[\"".$key."\"]"; } } eval("array_multisort(".$sort.");"); return $arr; } ?>
gebruik:
<?
/* gegevens */
$geg = Array();
$geg["id"] = Array(1,2,3,4,5);
$geg["naam"] = Array("name1","name2","name3","name4","name5");
$geg["value"] = Array("roos","maan","vis","aap","vogel");
print_r(sorteer($geg,"value","asc")); // arr '$geg' sorteren op 'value' (asc)
/* output:
Array
(
[id] => Array
(
[0] => 4
[1] => 2
[2] => 1
[3] => 3
[4] => 5
)
[naam] => Array
(
[0] => name4
[1] => name2
[2] => name1
[3] => name3
[4] => name5
)
[value] => Array
(
[0] => aap
[1] => maan
[2] => roos
[3] => vis
[4] => vogel
)
)
*/
?>
<? /* gegevens */ $geg["id"] = Array(1,2,3,4,5); $geg["naam"] = Array("name1","name2","name3","name4","name5"); $geg["value"] = Array("roos","maan","vis","aap","vogel"); print_r(sorteer ($geg,"value","asc")); // arr '$geg' sorteren op 'value' (asc) /* output: Array ( [id] => Array ( [0] => 4 [1] => 2 [2] => 1 [3] => 3 [4] => 5 ) [naam] => Array ( [0] => name4 [1] => name2 [2] => name1 [3] => name3 [4] => name5 ) [value] => Array ( [0] => aap [1] => maan [2] => roos [3] => vis [4] => vogel ) ) */ ?>
vlug een keer gescript
de clue is dus het gebruik van PHP.net: array_multisort. |