note 51366 deleted from function.sort by derick

From: Date: Wed, 31 Aug 2005 10:41:35 +0000
Subject: note 51366 deleted from function.sort by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-94510@lists.php.net to get a copy of this message
Note Submitter: hanje at hanje dot com ---- 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.

« previous php.notes (#94510) next »