Geen cache-versie.

Caching uitgeschakeld. Standaardinstelling voor deze pagina:ingeschakeld (code LNG204)
Als het scherm te langzaam is, kunt u de gebruikersmodus uitschakelen om de cacheversie te bekijken.

Rechercher une fonction PHP

array_splice

(PHP 4, PHP 5, PHP 7)

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

Description

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

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

Note:

Numerical keys in input are not preserved.

Note: If replacement is not an array, it will be typecast to one (i.e. (array) $replacement). This may result in unexpected behavior when using an object or NULL replacement.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Parameters

input

The input array.

offset

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

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

length

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

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

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

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

Tip

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

replacement

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.

Note:

Keys in the replacement array are not preserved.

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

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Return Values

Returns an array consisting of the extracted elements.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Examples

Example #1 array_splice() examples

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

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

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

$input = array("red""green""blue""yellow");
array_splice($input, -11, array("black""maroon"));
var_dump($input);
?>

The above example will output:

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"
}

Example #2 Equivalent statements to various array_splice() examples

The following statements are equivalent:

<?php

// append two elements to $input
array_push($input$x$y);
array_splice($inputcount($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($input01);

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

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

?>

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

See Also

Zoek een PHP-functie

Vertaling niet beschikbaar

De PHP-handleiding is nog niet in het Nederlands vertaald, dus het scherm is in het Engels. Als u wilt, kunt u het ook in het Frans of in het Duits raadplegen.

Als je de moed voelt, kun je je vertaling aanbieden ;-)

Nederlandse vertaling

U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.

Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.

Bij voorbaat dank.

Document heeft de 30/01/2003 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/php-rf-function.array-splice.html

De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.

Referenties

  1. Bekijk - html-document Taal van het document:fr Manuel PHP : http://php.net

Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.

Inhoudsopgave Haut