CKTIS / PHP
array_slice()
Copy a segment using positions in iteration order.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
array_slice(array $array, int $offset, ?int $length = null, bool $preserve_keys = false): arrayCopy a segment using positions in iteration order. A negative offset counts from the end; the offset is not an array key. A negative length leaves elements off the end. Integer keys are renumbered by default, so request key preservation when extracting a page of keyed records.
Signature
array_slice(array $array, int $offset, ?int $length = null, bool $preserve_keys = false): array
Example output
{"7":"B","9":"C"}
Examples
<?php
$rows = [5=>'A',7=>'B',9=>'C',12=>'D'];
echo json_encode(array_slice($rows, -3, -1, true));
Result
{"7":"B","9":"C"}