note 57223 deleted from ref.array by aidan
| From: | aidan@php.net | Date: | Sun, 09 Oct 2005 09:11:08 +0000 |
| Subject: | note 57223 deleted from ref.array by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-96723@lists.php.net to get a copy of this message | ||
Note Submitter: za at lombardiacom dot it
----
<?php
// Move an element inside an array preserving keys.
// This version is more efficient and less buggy :)
function array_move(&$array,$key,$position) {
if(!$position) return $array;
$from=array_search($key,array_keys($array));
$to=$from+$position;
$tot=count($array);
if($position>0) $to++;
if($to<0) $to=0;
else if($to>$tot) $to=$tot;
$head=array_slice($array,0,$to);
$tail=array_slice($array,$to,$tot-$to);
unset($head[$key],$tail[$key]);
$head[$key]=$array[$key];
return $head+$tail;
}
?>