array_slice

(PHP 4, PHP 5, PHP 7, PHP 8)

array_sliceExtraer una parte de un array

Descripción

array_slice(
    array $array,
    int $offset,
    int $length = null,
    bool $preserve_keys = false
): array

array_slice() devuelve la secuencia de elementos del array array tal y como se especifica en los parámetros offset y length.

Parámetros

array

El array de entrada.

offset

Si el índice dado por offset no es negativo, la secuencia empezará en esa posición del array. Si el offset es negativo, la secuencia empezará en esa posición empezando por el final del array.

length

Si la longitud dada por length es positiva, la secuencia tendrá hasta tantos elementos como indique el valor. Si el array es más corto que length, solamente estarán presentes los elementos disponibles del array. Si se proporciona length y es negativo, la secuencia finalizará en tantos elementos empezando por el final del array. Si se omite, entonces la secuencia contendrá todo el contenido desde offset hasta el final del array.

preserve_keys

Observe que array_slice() reordenará y reinicializará los índices numéricos del array de forma predeterminada. Se puede cambiar esta comportamiento estableciendo el parámetro preserve_keys a true.

Valores devueltos

Devuelve la parte del array. Si el índice es mayor que el tamaño del array, devuelve un array vacío.

Historial de cambios

Versión Descripción
5.2.4 El valor predeterminado del parámetro length se cambió a NULL. Una length NULL ahora indica a la función que use la longitud de array. Antes de esta versión, una length NULL se tomaba como de longitud cero (no se devolvía nada).
5.0.2 Se añadió el parámetro opcional preserve_keys.

Ejemplos

Ejemplo #1 Ejemplos de array_slice()

<?php
$entrada
= array("a", "b", "c", "d", "e");

$salida = array_slice($entrada, 2); // devuelve "c", "d", y "e"
$salida = array_slice($entrada, -2, 1); // devuelve "d"
$salida = array_slice($entrada, 0, 3); // devuelve "a", "b", y "c"

// observe las diferencias en las claves de los arrays
print_r(array_slice($entrada, 2, -1));
print_r(array_slice($entrada, 2, -1, true));
?>

El resultado del ejemplo sería:

Array
(
    [0] => c
    [1] => d
)
Array
(
    [2] => c
    [3] => d
)

Ver también

  • array_splice() - Elimina una porción del array y la reemplaza con otra cosa
  • unset() - Destruye una o más variables especificadas
  • array_chunk() - Divide un array en fragmentos

add a note

User Contributed Notes 17 notes

up
51
taylorbarstow at the google mail service
18 years ago
Array slice function that works with associative arrays (keys):

function array_slice_assoc($array,$keys) {
return array_intersect_key($array,array_flip($keys));
}
up
7
nathan dot fiscaletti at gmail dot com
6 years ago
If you want an associative version of this you can do the following:

function array_slice_assoc($array,$keys) {
return array_intersect_key($array,array_flip($keys));
}

However, if you want an inverse associative version of this, just use array_diff_key instead of array_intersect_key.

function array_slice_assoc_inverse($array,$keys) {
return array_diff_key($array,array_flip($keys));
}

Example:

$arr = [
'name' => 'Nathan',
'age' => 20,
'height' => 6
];

array_slice_assoc($arr, ['name','age']);

will return

Array (
'name' = 'Nathan',
'age' = 20
)

Where as

array_slice_assoc_inverse($arr, ['name']);

will return

Array (
'age' = 20,
'height' = 6
)
up
20
Ray.Paseur often uses Gmail
11 years ago
<?php
// CHOP $num ELEMENTS OFF THE FRONT OF AN ARRAY
// RETURN THE CHOP, SHORTENING THE SUBJECT ARRAY
function array_chop(&$arr, $num)
{
$ret = array_slice($arr, 0, $num);
$arr = array_slice($arr, $num);
return
$ret;
}
up
5
ted.devito at 9gmail9 dot 99com
16 years ago
based on worldclimb's arem(), here is a recursive array value removal tool that can work with multidimensional arrays.

function remove_from_array($array,$value){
$clear = true;
$holding=array();

foreach($array as $k => $v){
if (is_array($v)) {
$holding [$k] = remove_from_array ($v, $value);
}
elseif ($value == $v) {
$clear = false;
}
elseif($value != $v){
$holding[$k]=$v; // removes an item by combing through the array in order and saving the good stuff
}
}
if ($clear) return $holding; // only pass back the holding array if we didn't find the value
}
up
12
worldclimb at 99gmail99 dot com
16 years ago
array_slice can be used to remove elements from an array but it's pretty simple to use a custom function.

One day array_remove() might become part of PHP and will likely be a reserved function name, hence the unobvious choice for this function's names.

<?
function arem($array,$value){
$holding=array();
foreach($array as $k => $v){
if($value!=$v){
$holding[$k]=$v;
}
}
return $holding;
}

function akrem($array,$key){
$holding=array();
foreach($array as $k => $v){
if($key!=$k){
$holding[$k]=$v;
}
}
return $holding;
}

$lunch = array('sandwich' => 'cheese', 'cookie'=>'oatmeal','drink' => 'tea','fruit' => 'apple');
echo '<pre>';
print_r($lunch);
$lunch=arem($lunch,'apple');
print_r($lunch);
$lunch=akrem($lunch,'sandwich');
print_r($lunch);
echo '</pre>';
?>

(remove 9's in email)
up
9
developer at i-space dot org
22 years ago
remember that array_slice returns an array with the current element. you must use array_slice($array, $index+1) if you want to get the next elements.
up
6
s0i0m at dreamevilconcepts dot com
16 years ago
Using the varname function referenced from the array_search page, submitted by dcez at land dot ru. I created a multi-dimensional array splice function. It's usage is like so:

$array['admin'] = array('blah1', 'blah2');
$array['voice'] = array('blah3', 'blah4');
array_cut('blah4', $array);

...Would strip blah4 from the array, no matter where the position of it was in the array ^^ Returning this...

Array ( [admin] => Array ( [0] => blah1 [1] => blah2 ) [voice] => Array ( [0] => blah3 ) )

Here is the code...

<?php

function varname ($var)
{
// varname function by dcez at land dot ru
return (isset($var)) ? array_search($var, $GLOBALS) : false;
}

function
array_cut($needle, $haystack)
{
foreach (
$haystack as $k => $v)
{
for (
$i=0; $i<count($v); $i++)
if (
$v[$i] === $needle)
{
return
array_splice($GLOBALS[varname($haystack)][$k], $i, 1);
break; break;
}
}

?>

Check out dreamevilconcept's forum for more innovative creations!
up
1
Benjamin Sonntag
1 year ago
The documentation doesn't say it, but if LENGTH is ZERO, then the result is an empty array [].
up
1
kansey
9 years ago
To save the sort order of a numeric index in the array. Version php =>5.5.26
/*
Example
*/

$arr = array( "1" =>2, "2" =>3 , "3" =>5 );

print_r(array_slice($arr,1,null,true));

/*
Result

Array
(
[2] => 3
[3] => 5
)
*/
up
0
andreasblixt (at) msn (dot) com
19 years ago
<?php
// Combines two arrays by inserting one into the other at a given position then returns the result
function array_insert($src, $dest, $pos) {
if (!
is_array($src) || !is_array($dest) || $pos <= 0) return FALSE;
return
array_merge(array_slice($dest, 0, $pos), $src, array_slice($dest, $pos));
}
?>
up
-1
Anonymous
18 years ago
If you specify the fourth argument (to not reassign the keys), then there appears to be no way to get the function to return all values to the end of the array. Assigning -0 or NULL or just putting two commas in a row won't return any results.
up
-3
xananax at yelostudio dot com
13 years ago
<?php
/**
* Reorders an array by keys according to a list of values.
* @param array $array the array to reorder. Passed by reference
* @param array $list the list to reorder by
* @param boolean $keepRest if set to FALSE, anything not in the $list array will be removed.
* @param boolean $prepend if set to TRUE, will prepend the remaining values instead of appending them
* @author xananax AT yelostudio DOT com
*/
function array_reorder(array &$array,array $list,$keepRest=TRUE,$prepend=FALSE,$preserveKeys=TRUE){
$temp = array();
foreach(
$list as $i){
if(isset(
$array[$i])){
$tempValue = array_slice(
$array,
array_search($i,array_keys($array)),
1,
$preserveKeys
);
$temp[$i] = array_shift($tempValue);
unset(
$array[$i]);
}
}
$array = $keepRest ?
(
$prepend?
$array+$temp
:$temp+$array
)
:
$temp;
}

/** exemple ** /
$a = array(
'a' => 'a',
'b' => 'b',
'c' => 'c',
'd' => 'd',
'e' => 'e'
);
$order = array('c','b','a');

array_reorder($a,$order,TRUE);
echo '<pre>';
print_r($a);
echo '</pre>';
/** exemple end **/
?>
up
-2
aflavio at gmail dot com
17 years ago
/**
* Remove a value from a array
* @param string $val
* @param array $arr
* @return array $array_remval
*/
function array_remval($val, &$arr)
{
$array_remval = $arr;
for($x=0;$x<count($array_remval);$x++)
{
$i=array_search($val,$array_remval);
if (is_numeric($i)) {
$array_temp = array_slice($array_remval, 0, $i );
$array_temp2 = array_slice($array_remval, $i+1, count($array_remval)-1 );
$array_remval = array_merge($array_temp, $array_temp2);
}
}
return $array_remval;
}

$stack=Array('apple','banana','pear','apple', 'cherry', 'apple');
array_remval("apple", $stack);

//output: Array('banana','pear', 'cherry')
up
-2
Mr. P
16 years ago
Note that offset is not the same thing as key. Offset always starts at 0, while keys might be any number.

So this:

<?php print_r(array_slice(array(0 => 0, 5 => 5, 13 => 13),1)); ?>

will result in this:
Array
(
[0] => 5
[1] => 13
)
up
-3
aexchecker at yahoo dot com
17 years ago
<?php
/**
* @desc
* Combines two arrays by inserting one into the other at a given position then
* returns the result.
*
* @since 2007/10/04
* @version v0.7 2007/10/04 18:47:52
* @author AexChecker <AexChecker@yahoo.com>
* @param array $source
* @param array $destination
* @param int [optional] $offset
* @param int [optional] $length
* @return array
*/
function array_insert($source, $destination, $offset = NULL, $length = NULL) {
if (!
is_array($source) || empty($source)) {
if (
is_array($destination) && !empty($destination)) {
return
$destination;
}
return array();
}
if (
is_null($offset)) {
return
array_merge($destination, $source);
}
$offset = var2int($offset);
if (
is_null($length)) {
if (
$offset === 0) {
return
array_merge($source, array_slice($destination, 1));
}
if (
$offset === -1) {
return
array_merge(array_slice($destination, 0, -1), $source);
}
return
array_merge(
array_slice($destination, 0, $offset),
$source,
array_slice($destination, ++$offset)
);
}
if (
$offset === 0) {
return
array_merge($source, array_slice($destination, $length));
}
$destination_count = count($destination);
$length = var2int($length);
if (
$offset > 0) {
if (
$destination_count - $offset < 1) {
return
array_merge($destination, $source);
}
} else{
if ((
$t = $destination_count + $offset) < 1) {
return
array_merge($source, $destination);
}
$offset = $t;
}
if (
$length > 0) {
$length+= $offset;
} elseif (
$length < 0 && !($length * -1 < $destination_count)) {
return
$source;
} else {
$length = $offset;
}
return
array_merge(
array_slice($destination, 0, $offset),
$source,
array_slice($destination, $length)
);
}
?>
up
-2
bishop
19 years ago
Sometimes you need to pick certain non-integer and/or non-sequential keys out of an array. Consider using the array_pick() implementation below to pull specific keys, in a specific order, out of a source array:

<?php

$a
= array ('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4);
$b = array_pick($a, array ('d', 'b'));

// now:
// $a = array ('a' => 1, 'c' => '3');
// $b = array ('d' => 4, 'b' => '2');

function &array_pick(&$array, $keys)
{
if (!
is_array($array)) {
trigger_error('First parameter must be an array', E_USER_ERROR);
return
false;
}

if (! (
is_array($keys) || is_scalar($keys))) {
trigger_error('Second parameter must be an array of keys or a scalar key', E_USER_ERROR);
return
false;
}

if (
is_array($keys)) {
// nothing to do
} else if (is_scalar($keys)) {
$keys = array ($keys);
}

$resultArray = array ();
foreach (
$keys as $key) {
if (
is_scalar($key)) {
if (
array_key_exists($key, $array)) {
$resultArray[$key] = $array[$key];
unset(
$array[$key]);
}
} else {
trigger_error('Supplied key is not scalar', E_USER_ERROR);
return
false;
}
}

return
$resultArray;
}

?>
up
-4
delew
13 years ago
just a little tip.
to preserve keys without providing length: use NULL

array_slice($array, $my_offset, NULL, true);
To Top