array_slice()
function
Returns a portion of an array without modifying the original. Useful for pagination.
Syntax
array_slice(array $array, int $offset, ?int $length = null, bool $preserve_keys = false): array
Example
php
<?php
$items = ['a','b','c','d','e'];
print_r(array_slice($items, 1, 3)); // ['b','c','d']
print_r(array_slice($items, -2)); // ['d','e']
// Pagination
$page = 2; $perPage = 3;
$visible = array_slice($all, ($page-1)*$perPage, $perPage);