array_splice


php128 apg

RENOVE a portion of a given ARRAY and replace it with something else.





If $offset > 0 the start of the removed portion is at that offset from the beginning of the $input.

If $offset < 0 the start of the removed portion is at that offset from the end of the $input.

If $length is omitted, removes everything from $offset to the end of the array.

If $length = 0 no elements will be removed.

To remove everything from $offset to the end of the array when $replacement is also specified, use count($input) for $length.

If $replacement array is specified, then the removed elements are replaced with elements from this array.

If $offset and $length are such that nothing is removed, then the elements from the $replacement array are inserted in the place specified by the $offset.

Keys in the $replacement array are not preserved.

If $replacement is just one element it is not necessary to put array( ) or [ ] around it, unless the element is an array itself, an object or NULL.



<?php

arr array_splice 
arr &$input int $offset [, 
                                                 
int $length count($input) [, 
                                                
mix $replacement = array() ]] )
 

where,

&
$input The input ARRAY
                (
PASSED as reference )

$offset The position where the sequence will start
              
SEE the below TABLE )

$length The length of the sequence
              
SEE the below TABLE )

$replacement The element to replace the specified portion
                             
SEE the below TABLE )

?> 

&$input


The input ARRAY.



$offset


The position where the sequence will start.

VALUE MEANING
$offset > 0 Then the start of the removed portion is at that offset from the beginning of the $input.
$offset = 0 The sequence will start at the beginning of the $input.
$offset < 0 Then the start of the removed portion is at that offset from the end of the $input.
ed48


$length


The length of the sequence.

VALUE MEANING
$length = count($input) Remove everything from $offset to the end.
$length > 0 Remove everything from $offset to the end.
$length = 0 No elements will be removed.
$length < 0 Then the end of the removed portion
will be that many elements from the end of the $input.
ed48


$replacement


To control the reorder and reset the integer array indices.

TYPE MEANING
ARRAY Removed elements, replaced by elements of this array.
OBJECT Unexpected Behavior.
NULL
OTHER PROBABLY act as ARRAY.
ed48


  1 EXERCISE   

<?php

$arr01 
= [ "HSK" => [ "HUE""SATURATION""BLACK" ], 
              
"RGBc" => [ "RED""GREEN""BLUE" ],  
              
"RGBt" => [ "[0,255""[0,255]""[0,255]"  ] ];

echo 
'The given $arr01:<br>';
print_r($arr01);
echo 
'<br><br><br>';

echo 
'offset = ';
$offset01 = -48;
echo 
$offset01 '<br><br>';

$slc01 array_splice($arr01$offset01);

echo 
'array_splice($arr01, $offset01):<br>';
print_r($slc01);
echo 
'<br><br>';

echo 
'The resulting $arr01:<br>';
print_r($arr01);

?>

  2 EXERCISE   

<?php

$arr02 
=  [ "HSK" => [ "HUE""SATURATION""BLACK" ], 
              
"RGBc" => [ "RED""GREEN""BLUE" ],  
              
"RGBt" => [ "[0,255""[0,255]""[0,255]"  ] ]; 

echo 
'The given $arr02:<br>';
print_r($arr02);
echo 
'<br><br><br>';

echo 
'offset = ';
$offset02 mt_rand(-44);
echo 
$offset02 '<br><br>';

echo 
'length = ';
$length02 mt_rand(-44);
echo 
$length02 '<br><br>';

echo 
'replacement = ';
$repl02 'COLORS';
var_dump($repl02);
echo 
'<br><br>';

$slc02 array_splice($arr02$offset02$length02$repl02);

echo 
'array_splice($arr02, $offset02, $length02, $repl02):<br>';
print_r($slc02);
echo 
'<br><br>';

echo 
'The resulting $arr02:<br>';
print_r($arr02);

?>

  3 EXERCISE   

<?php

$arr03 
= [ 12345678910];

echo 
'The given $arr03:<br>';
print_r($arr03);
echo 
'<br><br><br>';

echo 
'offset = ';
$offset03 mt_rand(-1010);
echo 
$offset03 '<br><br>';

echo 
'length = ';
$length03 count($arr03);
echo 
$length03 '<br><br>';

$repl03 = [ 'one''two''three'
                  
'four''five''six'
                  
'seven''eight' 'nine''ten'];    

echo 
'replacement:<br>';
var_dump($repl03);
echo 
'<br><br>';

$slc03 array_splice($arr03$offset03$length03$repl03);

echo 
'The resulting $arr03:<br>';
print_r($arr03);

?>

  4 EXERCISE   

<?php

$actdwarfs 
= [ 'Sneezy'
                        
'Sleepy'
                        
'Dopey'
                        
'Doc'
                        
'Happy'
                        
'Bashful'
                        
'Grumpy' ];

$olddwarfs = [ 'Blick'
                       
'Flick'
                       
'Glick'
                       
'Snick'
                       
'Plick'
                       
'Whick'
                       
'Quee' ];

echo 
'The given $actdwarfs:<br>';
print_r($actdwarfs);
echo 
'<br><br><br>'

echo 
'The given $olddwarfs:<br>';
print_r($olddwarfs);
echo 
'<br><br><br>'

$spldwarfs array_splice($olddwarfs07$actdwarfs );

print_r($spldwarfs);
echo 
'<br><br><br>';

print_r($olddwarfs);
echo 
'<br><br><br>';

print_r($actdwarfs);

?>

  5 EXERCISE   

<?php

// The given $arr05d
$arr05d = [ "João""Jonas""José""Juvenal" ];

// element to add
$arr05e = [ "JULIANA""JULIA" ];

print_r($arr05d);
echo 
'<br><br>'

print_r($arr05e);
echo 
'<br><br>';

// initial position randomly collected
$pos05d mt_rand(-count($arr05d), count($arr05d));

echo 
'Position to consider: ' $pos05d;
echo 
'<br><br>';

// calculated length, to be considered
$lnt05d $pos05d count($arr05e);

echo 
'The lenght to consider: ' $lnt05d;
echo 
'<br><br>';

$spl05d array_splice($arr05d$pos05d$lnt05d$arr05e);

// element to remove
echo 'The element to be removed:<br>';
print_r($spl05d);
echo 
'<br><br>';

// resulting ARRAY
echo 'The resulting $arr05d:<br>';
print_r($arr05d);

?>

  6 EXERCISE   

<?php

/*
 * Function is implemented in ext/standard/array.c
*/

echo "Testing array_splice() basic operations.";
echo 
"<br><br>Test truncation:<br>";
$input = array("red""green""blue""yellow");
var_dump (array_splice($input2));
echo 
'<br><br>';
var_dump ($input);
// $input is now array("red", "green")

echo "<br><br><br>Test truncation with null length:<br>";
$input = array("red""green""blue""yellow");
var_dump (array_splice($input2null));
echo 
'<br><br>';
var_dump ($input);
// $input is now array("red", "green")

echo "<br><br><br>Test removing entries from the middle:<br>";
$input = array("red""green""blue""yellow");
var_dump (array_splice($input1, -1));
echo 
'<br><br>';
var_dump ($input);
// $input is now array("red", "yellow")

echo "<br><br><br>Test substitution at end:<br>";
$input = array("red""green""blue""yellow");
var_dump (array_splice($input1count($input), "orange"));
echo 
'<br><br>';
var_dump ($input);
// $input is now array("red", "orange")

$input = array("red""green""blue""yellow");
var_dump (array_splice($input, -11
                                           array(
"black""maroon")));
echo 
'<br><br>';
var_dump ($input);
// $input is now array("red", "green",
//          "blue", "black", "maroon")

echo "<br><br><br>Test insertion:<br>";
$input = array("red""green""blue""yellow");
var_dump (array_splice($input30"purple"));
echo 
'<br><br>';
var_dump ($input);
// $input is now array("red", "green",
//          "blue", "purple", "yellow");


?>

  7 EXERCISE   

<?php

/*
 * Function is implemented in ext/standard/array.c
*/

echo "Test behaviour when input array 
                      is in a reference set."
;

$input_array=array (array(1,2));
$input_array[]=&$input_array[0];
var_dump (array_splice ($input_array[0],1,1));
var_dump ($input_array);

echo 
"<br><br>Test behaviour of input 
                   arrays containing references:<br>"
;
/*
 *  There are three regions to test:, 
 * before cut, the cut and after the cut.
 *  For reach we check a plain value, 
 * a reference value with integer key and a
 *  reference value with a string key.
 */
 
$numbers=array(0,1,2,3,4,5,6,7,8,9,10,11,12);

$input_array=array(0,1,&$numbers[2],
                   
"three"=>&$numbers[3],
                   
4,
                   &
$numbers[5],
                   
"six"=>&$numbers[6],
                   
7,
                   &
$numbers[8],
                   
"nine"=>&$numbers[9]);
                   
var_dump (array_splice ($input_array,4,3));
var_dump ($input_array);

echo 
"<br><br>Test behaviour of replacement 
                        array containing references:<br>"
;

$three=3;
$four=4;
$input_array=array (0,1,2);
$b=array(&$three,"fourkey"=>&$four);
array_splice ($input_array,-1,1,$b);
var_dump ($input_array);

echo 
"<br><br>Test behaviour of replacement 
                     which is part of reference set:<br>"
;

$int=3;
$input_array=array (1,2);
$b=&$int;

array_splice ($input_array,-1,1,$b);
var_dump ($input_array);

?>

  8 EXERCISE   

<?php

/*
 * Function is implemented in ext/standard/array.c
*/

echo "array_splice() function : usage variations - 
                        lengths and offsets.<br><br>"
;


function 
test_splice ($offset$length)
{
    echo 
"<br><br>No replacement:<br>";
    
$input_array=array(0,1,2,3,4,5);
    
var_dump (array_splice ($input_array,
                                    
$offset,$length));
    
var_dump ($input_array);
    echo 
"<br><br>With replacement:<br>";
    
$input_array=array(0,1,2,3,4,5);
    
var_dump (array_splice ($input_array
                                    
$offset
                                    
$length
                                    array (
"A","B","C")));
    echo 
"<br><br>";                                
    
var_dump ($input_array);
    echo 
'<br><br><br>';
}

echo 
"01. absolute offset - absolute length - 
                        cut from beginning."
;
test_splice (0,2);
echo 
"02. absolute offset - absolute length - 
                         cut from middle."
;
test_splice (2,2);
echo 
"03. absolute offset - absolute length - 
                         cut from end."
;
test_splice (4,2);
echo 
"04. absolute offset - absolute length - 
                          attempt to cut past end."
;
test_splice (4,4);
echo 
"05. absolute offset - absolute length - 
                          cut everything."
;
test_splice (0,7);
echo 
"06. absolute offset - absolute length - 
                          cut nothing."
;
test_splice (3,0);

echo 
"07. absolute offset - relative length - 
                          cut from beginning."
;
test_splice (0,-4);

echo 
"08. absolute offset - relative length - 
                          cut from middle."
;
test_splice (2,-2);

echo 
"09. absolute offset - relative length - 
            attempt to cut form before beginning ."
;
test_splice (0,-7);

echo 
"10. absolute offset - relative length - 
                          cut nothing."
;
test_splice (2,-7);

echo 
"11. relative offset - absolute length - 
                          cut from beginning."
;
test_splice (-6,2);

echo 
"12. relative offset - absolute length - 
                          cut from middle."
;
test_splice (-4,2);
echo 
"13. relative offset - absolute length - 
                          cut from end."
;
test_splice (-2,2);
echo 
"14. relative offset - absolute length - 
                  attempt to cut past end."
;
test_splice (-2,4);
echo 
"15. relative offset - absolute length - 
                         cut everything."
;
test_splice (-6,6);
echo 
"16. relative offset - absolute length - 
                         cut nothing."
;
test_splice (-6,0);

echo 
"17. relative offset - relative length - 
                        cut from beginning."
;
test_splice (-6,-4);

echo 
"18. relative offset - relative length - 
                        cut from middle."
;
test_splice (-4,-2);

echo 
"19. relative offset - relative length - 
                        cut nothing."
;
test_splice (-4,-7);

?>

  9 EXERCISE   

<?php

/*
 * Function is implemented in ext/standard/array.c
*/

function test_splice ($replacement)
{
    
$input_array=array(0,1);
    
var_dump (array_splice ($input_array,2,0,$replacement));
    
var_dump ($input_array);
    echo 
'<br><br>';
}

test_splice (2);

test_splice (2.1);

test_splice (true);
//file type resource
$file_handle fopen(__FILE__"r");

test_splice ($file_handle);

fclose($file_handle);

?>