note 109397 added to function.array-unshift
| From: | thegrandoverseer | Date: | Fri, 13 Jul 2012 18:03:36 +0000 |
| Subject: | note 109397 added to function.array-unshift | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-189126@lists.php.net to get a copy of this message | ||
I wrote this function to sort the keys of an array using an array of keynames, in order.
<?php
/**
* function array_reorder_keys
* reorder the keys of an array in order of specified keynames; all other nodes not in $keynames
will come after last $keyname, in normal array order
* @param array &$array - the array to reorder
* @param mixed $keynames - a csv or array of keynames, in the order that keys should be reordered
*/
function array_reorder_keys(&$array, $keynames){
if(empty($array) || !is_array($array) || empty($keynames)) return;
if(!is_array($keynames)) $keynames = explode(',',$keynames);
if(!empty($keynames)) $keynames = array_reverse($keynames);
foreach($keynames as $n){
if(array_key_exists($n, $array)){
$newarray = array($n=>$array[$n]); //copy the node before unsetting
unset($array[$n]); //remove the node
$array = $newarray + array_filter($array); //combine copy with filtered array
}
}
}?>
----
Server IP: 69.147.83.197
Probable Submitter: 98.177.154.189
----
Manual Page -- http://www.php.net/manual/en/function.array-unshift.php
Edit -- https://master.php.net/note/edit/109397
Del: integrated -- https://master.php.net/note/delete/109397/integrated
Del: useless -- https://master.php.net/note/delete/109397/useless
Del: bad code -- https://master.php.net/note/delete/109397/bad+code
Del: spam -- https://master.php.net/note/delete/109397/spam
Del: non-english -- https://master.php.net/note/delete/109397/non-english
Del: in docs -- https://master.php.net/note/delete/109397/in+docs
Del: other reasons-- https://master.php.net/note/delete/109397
Reject -- https://master.php.net/note/reject/109397
Search -- https://master.php.net/manage/user-notes.php