Registriert seit: 10. Jun 2004
Ort: Garching (TUM)
4.579 Beiträge
|
Re: [php] array_multisort - hat das schon mal wer benutzt?
21. Jul 2005, 23:17
Ich glaube nicht, ich konnte es zumindest nicht der Beschreibung entnehmen
Vielleicht hilft dir der 7. Beitrag zu sort:
Zitat von: hanje at hanje dot com
29-Mar-2005 12:51
Code:
During the developping of a new website, I needed a way to sort array's using a value somewhere inside a multidomensional array, while preserving the rest of the array as provided.
This is what i came up with. Comments and ideas are more then welcome as this is my first contribution to the php website. Hope it's usefull to anybody out there, could spare you some time.
The comment inside the code should explain enough. You can call the function with $order being "asc" or "desc".
The function:
<?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];
}
?>
a usage example:
<?php
$input = array( "foo" => array(
"abc" => array('123'),
"pri" => array('456'),
"4"
),
"bar" => array(
array('123'),
"pri" => array('1'),
"abc"
)
);
$prior = array(''=>array('pri'=>array('')));
$blaat = multiSortAssocR($input,$prior);
echo print_r($blaat);
?>
and it's output:
Array
(
[bar] => Array
(
[0] => Array
(
[0] => 123
)
[pri] => Array
(
[0] => 1
)
[1] => abc
)
[foo] => Array
(
[abc] => Array
(
[0] => 123
)
[pri] => Array
(
[0] => 456
)
[0] => 4
)
)
As you can see, it only sorts the first dimension of the array, using the key specified in $prior as sorting index.
The rest of the array remains untouched.
|
|
Zitat
|