note 21773 added to function.array-splice

From: Date: Sun, 26 May 2002 11:02:41 +0000
Subject: note 21773 added to function.array-splice
Groups: php.notes 
Request: Send a blank email to php-notes+get-31045@lists.php.net to get a copy of this message
I believe the following is a version of array_slice that solves most of the issues for people that want an associative key offset, rather than an integer. function key_array_splice(&$input, $key_ofs, $length=NULL, $replacement=NULL) { // Adjust the length if it was negative or not passed if($length===NULL || $length<0) $count = $length+count($input); // Cycle through the array foreach($input as $key=>$value){ if(!$key_found){ if($key===$key_ofs){ $key_found=true; if($length!==NULL && $length>=0) $count=$length; if(is_array($replacement)) foreach($replacement as $r_key=>$r_value) $new_array[$r_key]=$r_value; }else $new_array[$key]=$value; } if($key_found){ if($count>0) $ret_array[$key]=$value; else $new_array[$key]=$value; } $count--; } // Finish up $input=$new_array; return $ret_array; } Note that this code needs PHP 4 for the use of the "===" and "!==" operators. -- http://www.php.net/manual/en/function.array-splice.php http://master.php.net/manage/user-notes.php?action=edit+21773 http://master.php.net/manage/user-notes.php?action=delete+21773 http://master.php.net/manage/user-notes.php?action=reject+21773

« previous php.notes (#31045) next »