arrayutils
  • Home
  • 📖How to use?
    • âš¡Installation
  • 📖Methods
    • âš¡Static method
      • static from()
      • static of()
      • static mapToArray()
    • âš¡Generic method
      • join()
      • every()
      • some()
      • reduce()
        • reduceRight()
      • sum()
      • pop()
      • shift()
      • includes()
      • keyExists()
      • indexOf()
      • find()
        • findIndex()
      • first()
        • keyFirst()
      • last()
        • keyLast()
      • random()
        • keyRandom()
      • splice()
    • âš¡Chain method
      • chunk()
      • column()
      • combine()
      • concat()
        • concatSoft()
      • countValues()
      • diff()
        • diffAssoc()
        • diffKey()
      • fill()
        • fillKeys()
      • filter()
      • flat()
        • flatMap()
      • flip()
      • forEach()
      • intersect()
        • intersectAssoc()
        • intersectKey()
      • keys()
      • map()
        • mapAssoc()
        • mapKey()
      • pad()
      • push()
      • replace()
      • reverse()
      • slice()
      • sort()
        • sortKey()
      • unique()
      • unshift()
      • values()
  • 📖Suffixes
    • âš¡Suffix - From
    • âš¡Suffix - As
  • links
    • 📌Github repo
    • 📌Packagist project
    • 📌Poggit project
Powered by GitBook
On this page
  • Syntax
  • Parameter
  • Return value
  • Prefixing
  • References
  1. 📖Methods
  2. ⚡Chain method

slice()

The slice() method returns an array with selected from start to end

Previousreverse()Nextsort()

Last updated 4 years ago

Example.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]

Syntax

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

Parameter

  • $start

    Zero-based index at which to start extraction.

    Default is 0.

  • $end

    Zero-based index at which to start extraction.

    Default is count($array).

  • $preserveKeys

    • When set to TRUE keys will be preserved. Default is FALSE which will re-index the chunk numerically

Return value

  • A sliced array.

Prefixing

$arrayUtils->sliceAs(int $start = 0, int $end = null, bool $preserve_keys = false) : array;
ArrayUtils::sliceFrom(iterable $from, int $start = 0, int $end = null, bool $preserve_keys = false) : ArrayUtils;
ArrayUtils::sliceFromAs(iterable $from, int $start = 0, int $end = null, bool $preserve_keys = false) : array;

References

PHP: array_slice - Manual
Logo
PHP: array_chunk - Manual
Logo