note 51366 added to function.sort
| From: | hanje at hanje dot com | Date: | Mon, 28 Mar 2005 22:51:54 +0000 |
| Subject: | note 51366 added to function.sort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-87113@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.sort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51366
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51366&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51366&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51366&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51366&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51366&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51366&report=yes
Search -- http://master.php.net/manage/user-notes.php