Esercitazione PHP

PHP HOME Introduzione a PHP Installazione PHP Sintassi PHP Commenti PHP Variabili PHP PHP Eco/Stampa Tipi di dati PHP Stringhe PHP Numeri PHP PHP matematica Costanti PHP Operatori PHP PHP Se...Altro...Altro Passaggio PHP Ciclo PHP Funzioni PHP Matrici PHP Superglobali PHP RegEx PHP

Moduli PHP

Gestione dei moduli PHP Convalida del modulo PHP Modulo PHP richiesto URL/e-mail del modulo PHP Modulo PHP completo

PHP avanzato

Data e ora PHP PHP Include Gestione dei file PHP Apri/Leggi file PHP Creazione/scrittura di file PHP Caricamento file PHP Cookie PHP Sessioni PHP Filtri PHP Filtri PHP avanzati Funzioni di callback PHP PHP JSON Eccezioni PHP

PHP OOP

PHP Che cos'è OOP Classi/Oggetti PHP Costruttore PHP PHP distruttore Modificatori di accesso PHP Ereditarietà PHP Costanti PHP Classi astratte PHP Interfacce PHP Tratti PHP Metodi statici PHP Proprietà statiche PHP Spazi dei nomi PHP Iterabili PHP

Database MySQL

Database MySQL MySQL Connect MySQL Crea DB MySQL Crea tabella Dati di inserimento MySQL MySQL Ottieni l'ultimo ID MySQL inserisce più MySQL preparato MySQL Seleziona dati MySQL dove MySQL Ordina per MySQL Elimina dati Dati di aggiornamento MySQL Dati limite MySQL

PHP XML

Parser XML PHP Analizzatore PHP SimpleXML PHP SimpleXML - Ottieni PHP XML espatriato PHP XML DOM

PHP - AJAX

Introduzione all'Ajax AJAX PHP Database AJAX XML AJAX Ricerca in tempo reale AJAX Sondaggio AJAX

Esempi PHP

Esempi PHP compilatore PHP Quiz PHP Esercizi PHP Certificato PHP

Riferimento PHP

Panoramica di PHP matrice PHP Calendario PHP Data PHP Directory PHP Errore PHP Eccezione PHP File system PHP Filtro PHP PHP FTP PHP JSON Parole chiave PHP PHP Libxml Posta PHP PHP matematica PHP Varie PHP MySQLi Rete PHP Controllo dell'output PHP RegEx PHP PHP SimpleXML Flusso PHP Stringa PHP Gestione delle variabili PHP Analizzatore XML PHP Zip PHP Fuso orario PHP

PHP array_slice() Funzione

❮ Riferimento all'array PHP

Esempio

Inizia la fetta dal terzo elemento dell'array e restituisce il resto degli elementi nell'array:

<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2));
?>

Definizione e utilizzo

La funzione array_slice() restituisce parti selezionate di un array.

Nota: se l'array ha chiavi di tipo stringa, l'array restituito conserverà sempre le chiavi (vedere l'esempio 4).


Sintassi

array_slice(array, start, length, preserve)

Valori dei parametri

Parameter Description
array Required. Specifies an array
start Required. Numeric value. Specifies where the function will start the slice. 0 = the first element. If this value is set to a negative number, the function will start slicing that far from the last element. -2 means start at the second last element of the array.
length Optional. Numeric value. Specifies the length of the returned array. If this value is set to a negative number, the function will stop slicing that far from the last element. If this value is not set, the function will return all elements, starting from the position set by the start-parameter.
preserve Optional. Specifies if the function should preserve or reset the keys. Possible values:
  • true - Preserve keys
  • false - Default. Reset keys


Dettagli tecnici

Valore di ritorno: Restituisce parti selezionate di un array
Versione PHP: 4+
Registro delle modifiche PHP: Il parametro preservare è stato aggiunto in PHP 5.0.2

Altri esempi

Esempio 1

Inizia la fetta dal secondo elemento dell'array e restituisci solo due elementi:

<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,1,2));
?>

Esempio 2

Utilizzando un parametro di avvio negativo:

<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,-2,1));
?>

Esempio 3

Con il parametro preserve impostato su true:

<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,1,2,true));
?>

Esempio 4

Con chiavi di stringa e intere:

<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"brown");
print_r(array_slice($a,1,2));

$a=array("0"=>"red","1"=>"green","2"=>"blue","3"=>"yellow","4"=>"brown");
print_r(array_slice($a,1,2));
?>

❮ Riferimento all'array PHP