This tutorial will show you how to extract different elements from an array. PHP has a function called array_slice() for that.
Let’s declare an array to be used in our example:
We will have in $first_3_users the first three elements from $users.
Using the above code we can get the last three elements.
array_slice() has three parameters:
- the array from which we want to extract the elements.
- start position.
- number of elements.
This function is very useful on pagination.
For example if we have a large array (hundreds of users or more) and we want to print 10 users on each page we can do: