PHP.nl

array_splice

array_splice

Remove a portion of the array and replace it with something else

array **array_splice** array $array int $offset  $length mixed $replacement

Removes the elements designated by and from the array, and replaces them with the elements of the array, if supplied. offset``length``array``replacement

Opmerking: > Numerical keys in are not preserved. array

Opmerking: > If is not an array, it will be

to one (i.e. ). This may result in unexpected
behavior when using an object or null .

replacementtypecast(array) $replacement``replacement

arrayThe input array.

offset If is positive then the start of the removed portion is at that offset from the beginning of the array. offset``array

   If  is negative then the start of the
   removed portion is at that offset from the end of the
    array.
  `offset``array`

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

   If  is specified and is positive,
   then that many elements will be removed.
  `length`


   If  is specified and is negative,
   then the end of the removed portion will be that many elements
   from the end of the array.
  `length`


   If  is specified and is zero,
   no elements will be removed.
  `length`

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

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

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

Opmerking: > Keys in the array are not preserved. replacement

   If  is just one element it is
   not necessary to put  or square brackets
   around it, unless the element is an array itself, an object or null.
  `replacement``array()`

Returns an array consisting of the extracted elements.

Voorbeeld: examples

<?php
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
var_dump($input);

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
var_dump($input);

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
var_dump($input);

$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
var_dump($input);
?>
array(2) {
  [0]=>
  string(3) "red"
  [1]=>
  string(5) "green"
}
array(2) {
  [0]=>
  string(3) "red"
  [1]=>
  string(6) "yellow"
}
array(2) {
  [0]=>
  string(3) "red"
  [1]=>
  string(6) "orange"
}
array(5) {
  [0]=>
  string(3) "red"
  [1]=>
  string(5) "green"
  [2]=>
  string(4) "blue"
  [3]=>
  string(5) "black"
  [4]=>
  string(6) "maroon"
}

Voorbeeld: Equivalent statements to various examples

The following statements are equivalent:

<?php

// append two elements to $input
array_push($input, $x, $y);
array_splice($input, count($input), 0, array($x, $y));

// remove the last element of $input
array_pop($input);
array_splice($input, -1);

// remove the first element of $input
array_shift($input);
array_splice($input, 0, 1);

// insert an element at the start of $input
array_unshift($input, $x, $y);
array_splice($input, 0, 0, array($x, $y));

// replace the value in $input at index $x
$input[$x] = $y; // for arrays where key equals offset
array_splice($input, $x, 1, $y);

?>

array_merge``array_slice``unset