<?php function multiSortAssocR($input = array(),$priority = array(), $order = 'asc'){
$priorityKeys = array(); $ouput = array();
//$input & $priority are array's, otherwise an php error will occur. No need for checking
// make tenporary array with key's and "priority" values that were found in the input array // return false on error. foreach ($input as $key => $val) if(!($priorityKeys[$key] = priorityFind(array($val),$priority))) return false;
// sort the temporatry priorityarray associative, reverse/normal according to $order beeing desc/asc if ($order == 'desc') arsort($priorityKeys); else asort($priorityKeys);
// everything has been done, now create output array which contains the original values of input array. // but now ordened with accoring to "priorityindex" foreach ($priorityKeys as $key => $val) $output[$key] = $input[$key];
return $output; }
function priorityFind($input = array(),$priority = array()){ //$input & $priority are ought to be array's, otherwise a php error will occur
// first, lets get the first key of $priority $priorityKey = key($priority);
//check whether we will continue diving into the priority array if (!is_array($priority[$priorityKey])) $priorityKey = $priority[$priorityKey];
// see if we can find the key specified in $priority, in the $input array. // if not, just get the first element if (array_key_exists($priorityKey,$input)) $inputKey = $priorityKey; else $inputKey = key($input);
// diving deeper into $priority and $input, let's just repeat this function till eternity and beyond (or when one of the arrays runs out) if (is_array($priority[$priorityKey])) return priorityFind($input[$inputKey],$priority[$priorityKey]); else return $input[$inputKey];
vb2: Saa vidt jeg kan see angives det ikke hvordan man ved sort laver den mest betydende sortering, da det jo er en division af to elementer i arrayet.
Okay, men ligger vel lidt under antagelsen at man kun vil sortere efter kolonner i arrayet, hvilket også er helt normalt. Men hvis du ser på det array og kode spørgsmålet blev oprettet med:
Det kan du jo ikke umiddelbart sortere. Derfor giver det nok god mening først at køre arrayet igennem en transformation der opretter en ratio-del, som man derefter kan sortere efter.
quote: "Derfor giver det nok god mening først at køre arrayet igennem en transformation der opretter en ratio-del, som man derefter kan sortere efter."
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.