Think of this function as substr() for arrays. You say where to start and how many elements to get (or don’t and get all to the end).

Negative values for offset and length are as they would be in substr() - they count from the end.

You can also set the fourth parameter to true if you want the keys preserved, though only numeric keys are affected.

$arr = array( 'apple',
              'banana',
              'carrot',
              'durian',
              'eggplant',
              'fennel',
              'g' => 'grapefruit',
              'honeydew',
              'iceburg lettuce',
              'jackfruit',
              'kale' );
 
$new = array_slice( $arr, 5 );
/*
array (
  0 => 'fennel',
  'g' => 'grapefruit',
  1 => 'honeydew',
  2 => 'iceburg lettuce',
  3 => 'jackfruit',
  4 => 'kale'
)
*/
 
$new = array_slice( $arr, 5, 3 );
/*
array (
  0 => 'fennel',
  'g' => 'grapefruit',
  1 => 'honeydew'
)
*/
 
$new = array_slice( $arr, 5, 3, true );
/*
array (
  5 => 'fennel',
  'g' => 'grapefruit',
  6 => 'honeydew'
)
*/
 
$new = array_slice( $arr, 5, -3 );
/*
array (
  0 => 'fennel',
  'g' => 'grapefruit',
  1 => 'honeydew'
)
*/
 
$new = array_slice( $arr, -5, 3 );
/*
array (
  'g' => 'grapefruit',
  0 => 'honeydew',
  1 => 'iceburg lettuce'
)
*/
 
$new = array_slice( $arr, -5, -3 );
/*
array (
  'g' => 'grapefruit',
  0 => 'honeydew'
)
*/