array_slice

(PHP 4 )

array_slice -- Extrait une portion de tableau

Description

array array_slice (array array, int offset [, int length])

array_slice() retourne une série d' élément du tableau array commencant à l'offset offset et représentant length éléments.

Si offset est positif, la série commencera à cet offset dans le tableau array. Si offset est négatif, cette série commencera à l'offset offset mais en commencant à la fin du tableau array.

Si length est donné et positif, alors la série aura autant d'éléments. Si length est donné et négatif, les éléments seront pris dans l'ordre inverse. Si length est omis, la séquence lira tous les éléments du tableau, depuis l'offset précisé jusqu'à la fin du tableau.

Exemple 1. Exemple avec array_slice()


<?php
$input = array ("a", "b", "c", "d", "e");
$output = array_slice ($input, 2);      // retourne "c", "d", et "e"
$output = array_slice ($input, 2, -1);  // retourne "c", "d"
$output = array_slice ($input, -2, 1);  // retourne "d"
$output = array_slice ($input, 0, 3);   // retourne "a", "b", et "c"
?>
      

Voir aussi array_splice().

Note : array_slice() a été ajoutée dans PHP 4.0.