note 57222 deleted from ref.array by levim

From: Date: Tue, 06 May 2014 22:02:24 +0000
Subject: note 57222 deleted from ref.array by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-199447@lists.php.net to get a copy of this message
Note Submitter: za at lombardiacom dot it ---- <?php // Swap 2 elements in array preserving keys. function array_swap(&$array,$key1,$key2) { $v1=$array[$key1]; $v2=$array[$key2]; $out=array(); foreach($array as $i=>$v) { if($i==$key1) { $i=$key2; $v=$v2; } else if($i==$key2) { $i=$key1; $v=$v1; } $out[$i]=$v; } return $out; } // Move an element inside an array preserving keys. function array_move(&$array,$key,$position) { $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-1; $n=0; $out=array(); foreach($array as $i=>$v) { if($n==$to) $out[$key]=$array[$key]; if($n++==$from) continue; $out[$i]=$v; } return $out; } ?>

« previous php.notes (#199447) next »