PHP array_slice() Function

In this chapter you will learn:

  1. Definition for PHP array_slice() Function
  2. Syntax for PHP array_slice() Function
  3. Parameter for PHP array_slice() Function
  4. Example - slice from 2
  5. Example - Start the slice from from the second array element, and return only two elements
  6. Example - Using a negative start parameter
  7. Example - With the preserve parameter set to true
  8. Example - With both string and integer keys

Definition

The array_slice() function returns selected parts of an array.

Syntax

PHP array_slice() Function has the following syntax.

array_slice(array,start,length,preserve)

Parameter

ParameterIs RequiredDescription
arrayRequired.Array to slice
startRequired.Starting point. 0 = the first element. negative value mean to slice from last element. -2 means start at the second last element of the array.
lengthOptional.length of the returned array. negative number means stop slicing that far from the last element. If not set, the function will return all elements, starting from start.
preserveOptional.preserve or reset the keys.

Possible values for preserve:

  • true - Preserve keys
  • false - Default. Reset keys

Example 1


<?php
    $a=array("A","B","C","D","java2s.com");
    print_r(array_slice($a,2));
?>

The code above generates the following result.

Example 2

Start the slice from from the second array element, and return only two elements:


<?php
    $a=array("A","B","C","D","java2s.com");
    print_r(array_slice($a,1,2));
?>

The code above generates the following result.

Example 3

Using a negative start parameter:


<?php
    $a=array("A","B","C","D","java2s.com");
    print_r(array_slice($a,-2,1));
?>

The code above generates the following result.

Example 4

With the preserve parameter set to true:


<?php
    $a=array("A","B","C","D","java2s.com");
    print_r(array_slice($a,1,2,true));
?>

The code above generates the following result.

Example 5

With both string and integer keys:


<?php//j  av  a  2s  .  com
    $a=array("a"=>"Java","b"=>"PHP","c"=>"java2s.com","d"=>"HTML","e"=>"java2s.com");
    print_r(array_slice($a,1,2));
    
    $a=array("0"=>"A","1"=>"B","2"=>"C","3"=>"D","4"=>"java2s.com");
    print_r(array_slice($a,1,2));
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP array_splice() Function
  2. Syntax for PHP array_splice() Function
  3. Parameter for PHP array_splice() Function
  4. Note for PHP array_splice() Function
  5. Example - Replace elements in an array
  6. Example - With the length parameter set to 0
  7. Example - inserts two new elements at the third position in the array
  8. Example - remove and insert elements at the same time
  9. Example - What happens if you omit the third argument
  10. Example - array_splice() automatically casts the fourth argument to an array before using it
Home » PHP Tutorial » PHP Array Functions
PHP array() function
PHP array_change_key_case() function
PHP array_chunk() function
PHP array_combine() function
PHP array_count_values() function
PHP array_diff() function
PHP array_diff_assoc() Function
PHP array_diff_key() function
PHP array_diff_uassoc() function
PHP array_diff_ukey() function
PHP array_fill() function
PHP array_fill_keys() function
PHP array_filter() function
PHP array_flip() function
PHP array_intersect() Function
PHP array_intersect_assoc() Function
PHP array_intersect_key() function
PHP array_intersect_uassoc() Function
PHP array_intersect_ukey() Function
PHP array_key_exists() Function
PHP array_keys() function
PHP array_map() Function
PHP array_merge() function
PHP array_merge_recursive() Function
PHP array_multisort() Function
PHP array_pad() Function
PHP array_pop() function
PHP array_product() Function
PHP array_push() function
PHP array_rand() function
PHP array_reduce() Function
PHP array_replace() Function
PHP array_replace_recursive() Function
PHP array_reverse() Function
PHP array_search() Function
PHP array_shift() Function
PHP array_slice() Function
PHP array_splice() Function
PHP array_sum() Function
PHP array_udiff() Function
PHP array_udiff_assoc() Function
PHP array_udiff_uassoc() Function
PHP array_uintersect() Function
PHP array_uintersect_assoc() Function
PHP array_uintersect_uassoc() Function
PHP array_unique() Function
PHP array_unshift() Function
PHP array_values() Function
PHP array_walk() Function
PHP array_walk_recursive() Function
PHP arsort() Function
PHP asort() Function
PHP compact() Function
PHP count() Function
PHP current() Function
PHP each() Function
PHP end() Function
PHP explode() Function
PHP extract() Function
PHP implode() Function
PHP in_array() Function
PHP key() Function
PHP krsort() Function
PHP ksort() Function
PHP list() Function
PHP natcasesort() Function
PHP natsort() Function
PHP next() Function
PHP pos() Function
PHP prev() Function
PHP range() Function
PHP reset() Function
PHP rsort() Function
PHP shuffle() Function
PHP sizeof() Function
PHP sort() Function
PHP uasort() Function
PHP uksort() Function
PHP usort() Function