note 40034 added to function.array-splice
| From: | bitmore dot co dot kr at rn2 dot php dot net | Date: | Thu, 19 Feb 2004 15:09:49 +0000 |
| Subject: | note 40034 added to function.array-splice | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-65401@lists.php.net to get a copy of this message | ||
//Original and Source - Modify
paule at cs dot tamu dot edu
26-May-2002 08:02
//Compare Source -
//richard at richard-sumilang dot com
//23-Nov-2002 04:27
//IndexKey Campare Sample Test
///Other Sample
function key_array_splice(&$input, $key_ofs, $length=NULL, $replacement=NULL) {
if($length===NULL || $length<0) $count = $length+count($input);
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 if( $replacement !== NULL ) $new_array[$key] = $replacement;
}
}
if($key_found){
if($count>0) $ret_array[$key]=$value;
else $new_array[$key]=$value;
}
$count--;
}
$input=$new_array;
return $ret_array;
}
$test = array(1,2,3,4,5);
print_r( key_array_splice( $test,0 ) );echo "<br>";//Array ( [0] => 1 [1] =>
2 [2] => 3 [3] => 4 [4] => 5 )
$test = array(1,2,3,4,5);
print_r( key_array_splice( $test,0,1 ) );echo "<br>";//Array ( [0] => 1 )
print_r( $test );echo "<br>";//Array ( [1] => 2 [2] => 3 [3] => 4 [4]
=> 5 )
$test = array(1,2,3,4,5);
print_r( key_array_splice( $test,0,1,"Deck" ) );echo "<br>";//Array ( [0]
=> 1 )
print_r( $test );echo "<br>";//Array ( [0] => Deck [1] => 2 [2] => 3 [3]
=> 4 [4] => 5 )
----
Manual Page -- http://www.php.net/manual/en/function.array-splice.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+40034
Delete -- http://master.php.net/manage/user-notes.php?action=delete+40034&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+40034&report=yes
Search -- http://master.php.net/manage/user-notes.php