Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>PHP has a built - in function, array_slice() , that you can use to extract a range of elements from an array. To use it, pass it the array to extract the slice from, followed by the position of the first element in the range (counting from zero), followed by the number of elements to extract. The function returns a new array containing copies of the elements you extracted (it doesn ’ t touch the original array). For example:</p> <pre><code>$authors = array( “Steinbeck”, “Kafka”, “Tolkien”, “Dickens” ); $authorsSlice = array_slice( $authors, 1, 2 ); // Displays “Array ( [0] = &gt; Kafka [1] = &gt; Tolkien )” print_r( $authorsSlice ); </code></pre> <p>By the way, if you leave out the third argument to array_slice() , the function extracts all elements from the start position to the end of the array:</p> <pre><code>$authors = array( “Steinbeck”, “Kafka”, “Tolkien”, “Dickens” ); $authorsSlice = array_slice( $authors, 1 ); // Displays “Array ( [0] = &gt; Kafka [1] = &gt; Tolkien [2] = &gt; Dickens )”; print_r( $authorsSlice ); </code></pre> <p>Earlier you learned that array_slice() doesn ’ t preserve the indices of elements taken from an indexed array. If you want to preserve the indices, you can pass a fourth argument, true , to array_slice() :</p> <pre><code>$authors = array( “Steinbeck”, “Kafka”, “Tolkien”, “Dickens” ); // Displays “Array ( [0] = &gt; Tolkien [1] = &gt; Dickens )”; print_r( array_slice( $authors, 2, 2 ) ); // Displays “Array ( [2] = &gt; Tolkien [3] = &gt; Dickens )”; print_r( array_slice( $authors, 2, 2, true ) ); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload