# slice()

{% code title="Example.php" %}

```php
<?php use kim\present\utils\arrays\ArrayUtils;

$arrayUtils = ArrayUtils::from(range(1, 10));

$arrayUtils->slice(2);    // expected output: [3, 4, 5, 6, 7, 8, 9, 10]
$arrayUtils->slice(2, 4); // expected output: [3, 4]
$arrayUtils->slice(-4);   // expected output: [7, 8, 9, 10]
```

{% endcode %}

## Syntax

```php
$arrayUtils->slice(int $start = 0, int $end = null, bool $preserve_keys = false) : ArrayUtils;
```

### Parameter

* `$start` <img src="/files/-MKk9YjCz_YkVR1YiRy-" alt="" data-size="line">&#x20;

  > Zero-based index at which to start extraction.
  >
  > Default is `0`.
* `$end` <img src="/files/-MKk9YjCz_YkVR1YiRy-" alt="" data-size="line">&#x20;

  > Zero-based index at which to start extraction.
  >
  > Default is `count($array)`.
* `$preserveKeys` <img src="/files/-MKk9YjCz_YkVR1YiRy-" alt="" data-size="line">&#x20;
  * When set to **`TRUE`** keys will be preserved. \
    Default is **`FALSE`** which will re-index the chunk numerically

### Return value

* A sliced array.

## Prefixing

```php
$arrayUtils->sliceAs(int $start = 0, int $end = null, bool $preserve_keys = false) : array;
```

```php
ArrayUtils::sliceFrom(iterable $from, int $start = 0, int $end = null, bool $preserve_keys = false) : ArrayUtils;
```

```php
ArrayUtils::sliceFromAs(iterable $from, int $start = 0, int $end = null, bool $preserve_keys = false) : array;
```

## References

{% embed url="<https://www.php.net/manual/en/function.array-slice.php>" %}

{% embed url="<https://www.php.net/manual/en/function.array-chunk>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://arrayutils.docs.present.kim/methods/c/slice.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
