Voorbeeld, ik wil de children_array hier sorteren op "eenheid_int" en wel oplopend:
aanmaken:
Code: Selecteer alles
$children_array = array();
while($row = mysql_fetch_array($result)) {
$children_array[] = array('id' => $row['id'],'eenheid_int' => intval($row['eenheid']), 'eenheid_str' => $row['eenheid'], 'prijs' =>$row['prijs']);
}
$children_array2 = sortmddata($children_array,'eenheid_int','ASC','num');
de functie:
Code: Selecteer alles
///////////////////////////////////////////////////////////////////////////////////////////////////////
// sort
function sortmddata($array, $by, $order, $type){
//$array: the array you want to sort
//$by: the associative array name that is one level deep
////example: name
//$order: ASC or DESC
//$type: num or str
$sortby = "sort$by"; //This sets up what you are sorting by
$firstval = current($array); //Pulls over the first array
$vals = array_keys($firstval); //Grabs the associate Arrays
foreach ($vals as $init){
$keyname = "sort$init";
$$keyname = array();
}
//This was strange because I had problems adding
//Multiple arrays into a variable variable
//I got it to work by initializing the variable variables as arrays
//Before I went any further
foreach ($array as $key => $row) {
foreach ($vals as $names){
$keyname = "sort$names";
$test = array();
$test[$key] = $row[$names];
$$keyname = array_merge($$keyname,$test);
}
}
//This will create dynamic mini arrays so that I can perform
//the array multisort with no problem
//Notice the temp array... I had to do that because I
//cannot assign additional array elements to a
//varaiable variable
if ($order == "DESC"){
if ($type == "num"){
array_multisort($$sortby,SORT_DESC, SORT_NUMERIC,$array);
} else {
array_multisort($$sortby,SORT_DESC, SORT_STRING,$array);
}
} else {
if ($type == "num"){
array_multisort($$sortby,SORT_ASC, SORT_NUMERIC,$array);
} else {
array_multisort($$sortby,SORT_ASC, SORT_STRING,$array);
}
}
//This just goed through and asks the additional arguments
//What they are doing and are doing variations of
//the multisort
return $array;
}
// sort
///////////////////////////////////////////////////////////////////////////////////////////////////////