# Home

![](https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MKYhyHaArG9Gsnmdxc8%2Fuploads%2F2Ig1JFZY1AJWkWysXHj1%2Ficon.svg?alt=media\&token=3f37e77e-3501-48a5-a02a-65afd51c4e5e)

## **ArrayUtils**

#### Awesome array manipulation utility for PHP

## :hash:What is ArrayUtils?

ArrayUtils is a library that provides a great way to manipulate arrays.

The ~~evil~~ PHP array functions give developers the pain of:

* Some functions **requires array first**, but some functions **require array last**... &#x20;
* Some functions **returns result**, but some functions **modify referenced variables**...
* Code was **line-break** because since it is a the function...
* Each function has **different parameters to the callback function**...
* No modern functions using arrays. Such as `every`, `some`

I created this library to solve these problems and make code flow like `js-array`

## :hash:What happens when I use it? <a href="#importing" id="importing"></a>

Processing arrays through this library makes the code flow much clearer than pure PHP.

Pure PHP's array functions, when nested, reverse the code and get deeper and deeper indentations.

ArrayUtils solves these problems and makes the code easier to figure out.

Also, if you Using the `arrow-function` added in PHP 7.4, you can write more neatly.

{% tabs %}
{% tab title="Pure PHP" %}

```php
$playerFiles = scandir(Server::getInstance()->getDataPath() . "players/";
$onlinePlayers = Server::getInstance()->getOnlinePlayers();

$onlineNames = array_column(
    array_map(
        function(Player $player) : array{ return [strtolower($player->getName()), $player->getName()]; },
        $onlinePlayers
    ), 1, 0
);
$playerNames = array_column(
    array_map(
        function(string $playerName) : array{ return [strtolower($playerName), $playerName]; },
        array_map(
            function(string $playerName) use($onlineNames) : string{ return $onlineNames[strtolower($playerName)] ?? $playerName; },
            array_map(
                function(string $fileName) : string{ return substr($fileName, 0, -strlen(\".dat\")); },
                array_filter($playerFiles, function(string $fileName) : bool{ return substr($fileName, -strlen(\".dat\")) === \".dat\"; })
            )
        )
    ), 1, 0
);
```

{% endtab %}

{% tab title="with ArrayUtils" %}

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

$playerFiles = scandir(Server::getInstance()->getDataPath() . "players/");
$onlinePlayers = Server::getInstance()->getOnlinePlayers();

$onlineNames = ArrayUtils::mapAssocFromAs($onlinePlayers, function(Player $player) : array{ return [strtolower($player->getName()), $player->getName()]; });
$playerNames = ArrayUtils::filterFrom($playerFiles, function(string $fileName) : bool{ return substr($fileName, -strlen(".dat")) === ".dat"; }) 
    ->map(function(string $fileName) : string{ return substr($fileName, 0, -strlen(".dat")); })
    ->map(function(string $playerName) use($onlineNames) : string{ return $onlineNames[strtolower($playerName)] ?? $playerName; })
    ->mapAssocAs(function(string $playerName) : array{ return [strtolower($playerName), $playerName]; });
```

{% endtab %}

{% tab title="PHP >= 7.4 with ArrayUtils" %}

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

$playerFiles = scandir(Server::getInstance()->getDataPath() . "players/");
$onlinePlayers = Server::getInstance()->getOnlinePlayers();

$onlineNames = ArrayUtils::mapAssocFromAs($onlinePlayers, fn(Player $player) => [strtolower($player->getName()), $player->getName()]);
$playerNames = ArrayUtils::filterFrom($playerFiles, fn(string $fileName) => substr($fileName, -strlen(".dat")) === ".dat") 
    ->map(fn(string $fileName) => substr($fileName, 0, -strlen(".dat")))
    ->map(fn(string $playerName) => $onlineNames[strtolower($playerName)] ?? $playerName)
    ->mapAssocAs(fn(string $playerName) => [strtolower($playerName), $playerName]);
```

{% endtab %}
{% endtabs %}


# How to use?

{% hint style="info" %}
The guide assumes intermediate level knowledge of **PHP-language** and [**poggit-virion**](https://github.com/poggit/support/blob/master/virion.md)
{% endhint %}

The easiest way to try out ArrayUtils is using the poggit. Poggit automatically merges virions, it is better to use this feature.

## :hash:Importing ArrayUtils <a href="#importing" id="importing"></a>

As with all classes, must import ArrayUtils into your php file.

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

## :hash:Create ArrayUtils from value <a href="#creating" id="creating"></a>

There are 4 ways to create ArrayUtils.

### ⚡ 1. Use constructor

> In the most basic way, it's just created through the constructor.
>
> ```php
> $arr = new ArrayUtils([1,2,3,4,5]);
> echo $arr->join(", "); //1, 2, 3, 4, 5
> ```

### ⚡ 2. Use static from() method

> Same method as [`Array.from()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from) in java script
>
> ```php
> $arr = ArrayUtils::from([1,2,3,4,5]);
> echo $arr->join(", "); //1, 2, 3, 4, 5
> ```

### ⚡ 3. Use static of() method

> Same method as [`Array.of()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of) in java script
>
> ```php
> $arr = ArrayUtils::of(1,2,3,4,5);
> echo $arr->join(", "); //1, 2, 3, 4, 5
> ```

### ⚡ 4. Use magic suffix "From"

> > For a detailed description of the `from-suffix`, [click here](https://arrayutils.docs.present.kim/methods/main#from-suffix)
>
> All methods can be called statically with the suffix `From`
>
> ```php
> echo ArrayUtils::joinFrom([1,2,3,4,5], ", "); //1, 2, 3, 4, 5
> ```

## :hash:Use the desired methods <a href="#using" id="using"></a>

{% content-ref url="/pages/-MKjzUNaOFnOZ6kW1B1-" %}
[📖Methods](/methods)
{% endcontent-ref %}


# ⚡Installation

## Install with poggit

See [poggit/support/virion](https://github.com/poggit/support/blob/master/virion.md#compiling-a-virion-with-poggit)

{% hint style="info" %}
I will write a detailed explanation later. You can use it according to the above link.
{% endhint %}

## Install with composer

{% hint style="warning" %}
I'll be registering with packagist, so you can use it later through Composer.
{% endhint %}


# 📖Methods

{% hint style="info" %}

#### ArrayUtils has numerous methods.

#### Each methods are divided into three main types depending on supporting prefix.

{% endhint %}

## ⚡Static method

{% content-ref url="/pages/-MKpFi0sdXDCjX5k7lP\_" %}
[⚡Static method](/methods/s)
{% endcontent-ref %}

{% hint style="danger" %}

### Not support method prefix

{% endhint %}

## ⚡Generic method

{% content-ref url="/pages/-MKpFam-p14kqQff2AZR" %}
[⚡Generic method](/methods/g)
{% endcontent-ref %}

{% hint style="warning" %}

### Support only "From" prefix

{% endhint %}

## ⚡Chain method

{% content-ref url="/pages/-MKpFkPN8XQmE3omaVn7" %}
[⚡Chain method](/methods/c)
{% endcontent-ref %}

{% hint style="success" %}

### Support all prefix

{% endhint %}


# ⚡Static method

The static-method is a static method without polymorphism.

## :bookmark:Support prefix

{% hint style="danger" %}

## Static method not support method prefix

{% endhint %}

## :bookmark:Method list

{% content-ref url="/pages/-MKjzUNbOQ3vIJM3-Nu\_" %}
[static from()](/methods/s/from)
{% endcontent-ref %}

{% content-ref url="/pages/-MKjzUNcEq7ZdWEvCHrU" %}
[static of()](/methods/s/of)
{% endcontent-ref %}

{% content-ref url="/pages/-MKjzUNduQa9MtpMZytG" %}
[static mapToArray()](/methods/s/maptoarray)
{% endcontent-ref %}


# static from()

The from() static method creates a new, shallow-copied ArrayUtils instance from an iterable.

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

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

var_export(ArrayUtils::from([3,6,9]));
// expected output: ArrayUtils(array(3, 6, 9))

var_export(ArrayUtils::from([1, 2, 3], function($x){ return $x + $x; }));
// expected output: ArrayUtils(array(2, 4, 9))
```

{% endcode %}

## Syntax

```php
ArrayUtils::from(iterable $iterable, ?callable $mapFn = null) : ArrayUtils
```

### Parameter

* `$iterable`&#x20;

  > Iterable object to convert to an array.
* `$mapFn`  <img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;

  > Map function to call on every element of the array.\
  > Default is `NULL` . If is null, not execute map function.

### Return value

* A new `ArrayUtils` instance.

## References

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from>" %}


# static of()

The of() static method creates a new, ArrayUtils instance from variadic function arguments

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

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

ArrayUtils::of(3,6,9);
// expected output: ArrayUtils(array(3, 6, 9))
```

{% endcode %}

## Syntax

```php
ArrayUtils::of(mixed ...$elements) : ArrayUtils
```

### Parameter

* `...$elements`&#x20;

  > Elements used to create the array.

### Return value

* A new `ArrayUtils` instance.

## References

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of>" %}


# static mapToArray()

The mapToArray() static method cast all elements of the iterable to an array

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

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

ArrayUtils::mapToArray([
    1,
    [2, 3],
    new class()extends \StdClass{public $arrayStart = 0;},
    new ArrayObject([1, 2, 3, 4, 5]),
    new ArrayIterator(["ArrayUtils", "mapToArray"])
]));
// expected output: Array(
//                      Array(1),
//                      Array(2, 3),
//                      Array("arrayStart" => 0),
//                      Array(1, 2, 3, 4, 5),
//                      Array("ArrayUtils", "mapToArray")
//)
```

{% endcode %}

## Syntax

```php
ArrayUtils::mapToArray(iterable $iterables) : array
```

### Parameter

* `$iterable`

  > An iterable containing an iterable. It converted to an array.

### Return value

* A new array containing elements converted to an array.

## References

{% embed url="<https://www.php.net/manual/en/language.types.iterable>" %}


# ⚡Generic method

The generic-methods is a member methods that do not return ArrayUtils.

> The generic-method is returns an single result.
>
> Because of this, method chaining stops when this type of method is called.

## :bookmark:Support prefix

{% content-ref url="/pages/-MKpIDQbxjHxelU6Oe9-" %}
[Suffix - From](/prefixs/from)
{% endcontent-ref %}

## :bookmark:Method list

{% content-ref url="/pages/-MKmSpTl0tivPHTUl7cn" %}
[join()](/methods/g/join)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTTSkjoDCow9Qfs" %}
[every()](/methods/g/every)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTWDb\_AvKOx13If" %}
[some()](/methods/g/some)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpThy4vgovSVrqFM" %}
[reduce()](/methods/g/reduce)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTiRNkoVGsmZkFJ" %}
[reduceRight()](/methods/g/reduce/right)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTXRBabb2Q1p3hy" %}
[sum()](/methods/g/sum)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmWPwS70KXXxps2oUH" %}
[pop()](/methods/g/pop)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTk1c5qJUYH8VCn" %}
[shift()](/methods/g/shift)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTUrWDNd70gCjOG" %}
[includes()](/methods/g/includes)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTVd8qh9mJFifRp" %}
[keyExists()](/methods/g/key-exists)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTbJlxmNLHbGD6A" %}
[indexOf()](/methods/g/index-of)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTY4wig2FZqHKfU" %}
[find()](/methods/g/find)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTZn2laP8ct\_3Z5" %}
[findIndex()](/methods/g/find/index)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpT\_lHT9ReqinmNP" %}
[first()](/methods/g/first)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTa1oFmgPQhc6w\_" %}
[keyFirst()](/methods/g/first/key)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTcDwmpQyz5najf" %}
[last()](/methods/g/last)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTdviaS9GmMSfM8" %}
[keyLast()](/methods/g/last/key)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTfxJjTxNxN3ujJ" %}
[random()](/methods/g/random)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTgjC9PXAeAFv0N" %}
[keyRandom()](/methods/g/random/key)
{% endcontent-ref %}


# join()

The join() method join array elements with a string

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

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

$arrayUtils = ArrayUtils::from(["Apple", "Banana", "Carrot"]);

//Joining array with multiple elements
$arrayUtils->join();         // "Apple,Banana,Carrot"
$arrayUtils->join(" and ");  // "Apple and Banana and Carrot"
$arrayUtils->join(" || ");   // "Apple || Banana || Carrot"

//Joining with prefix
$arrayUtils->join(" and ", "I like ");
// expected output: "I like  Apple and Banana and Carrot"

//Joining with prefix and suffix
$arrayUtils->join(" and ", "I like ", " little bit");
// expected output: "I like Apple and Banana and Carrot little bit"
```

{% endcode %}

## Syntax

```php
$arrayUtils->join(string $glue = ",", string $prefix = "", string $suffix = "") : string;
```

### Parameter

* `$glue` <img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;

  > The string to separate each pair of adjacent elements.\
  > Default: `","`
* `$prefix` <img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;

  > The prefix string.\
  > Default: `""`(empty)
* `$suffix`<img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;

  > The suffix string.\
  > Default: `""`(empty)

### Return value

* A string with all array elements joined

## Prefixing

```php
ArrayUtils::joinFrom(iterable $from, string $glue = ",", string $prefix = "", string $suffix = "") : string;
```

## References

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

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join>" %}


# every()

The every() method tests whether all elements pass the function

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

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

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

//Test if all elements are less than 40
$arrayUtils->every(function($num){ return $num < 40; });
// expected output: true

//Test if all elements are less than 30
$arrayUtils->every(function($num){ return $num < 40; });
// expected output: false
```

{% endcode %}

## Syntax

```php
$arrayUtils->every(callable $callback) : bool;
```

### Parameter

* `$callback`

  > A function to test for each element, taking three arguments:
  >
  > * `$value` The current element being processed in the array.
  > * `$key` The index of the current element being processed in the array.
  > * `$array`  The array `every` was called upon.

### Return value

* A `boolean` the whether all elements pass the function

## Prefixing

```php
ArrayUtils::everyFrom(iterable $from, callable $callback) : bool;
```

## References

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every>" %}


# some()

The some() method tests whether least one element pass the function

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

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

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

//Test if any elements is same to 20
$arrayUtils->some(function($num){ return $num === 20; });
// expected output: true

//Test if any elements is same to 40
$arrayUtils->some(function($num){ return $num === 40; });
// expected output: false
```

{% endcode %}

## Syntax

```php
$arrayUtils->some(callable $callback) : bool;
```

### Parameter

* `$callback`

  > A function to test for each element, taking three arguments:
  >
  > * `$value` The current element being processed in the array.
  > * `$key` The index of the current element being processed in the array.
  > * `$array`  The array `every` was called upon.

### Return value

* A `boolean` the whether any elements pass the function

## Prefixing

```php
ArrayUtils::someFrom(iterable $from, callable $callback) : bool;
```

## References

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some>" %}


# reduce()

The reduce() method iteratively reduce the array to a single value using a callback function

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

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

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

//This is like a 10 factorial
$arrayUtils->reduce(function($accumulator, $value){ return $accumulator * $value; }, 1);
// expected output: 3628800



$arrayUtils->reduce(
    function($accumulator, $value){
        echo  "$accumulator * $value = " . ($accumulator * $value) . PHP_EOL;
        return $accumulator * $value; }, 1));
//echo 1 * 1 = 1
//echo 1 * 2 = 2
//echo 2 * 3 = 6
//echo 6 * 4 = 24
//echo 24 * 5 = 120
//echo 120 * 6 = 720
//echo 720 * 7 = 5040
//echo 5040 * 8 = 40320
//echo 40320 * 9 = 362880
//echo 362880 * 10 = 3628800
//expected output: 3628800
```

{% endcode %}

## Syntax

```php
$arrayUtils->reduce(callable $callback, mixed $initialValue = null) : mixed;
```

### Parameter

* `$callback`

  > A function that produces an element of the new Array, taking four arguments:
  >
  > * `$accumulator`The accumulator accumulates callback's return values. It is the accumulated value previously returned in the last invocation of the callback—or `$initialValue`, if it was supplied (see below).
  > * `$value` The current element being processed in the array.
  > * `$key` The index of the current element being processed in the array.
  > * `$array`  The array `every` was called upon.
* `$initialValue` <img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;
  * A value to use as the first argument to the first call of the callback.

### Return value

* The result value.

## Prefixing

```php
ArrayUtils::reduceFrom(iterable $from, callable $callback, mixed $initialValue = null) : mixed;
```

## References

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

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce>" %}


# reduceRight()

The reduceRight() method all similar to reduce(), but It works in reverse order.

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

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

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

//This is like a 10 factorial
$arrayUtils->reduceRight(function($accumulator, $value){ return $accumulator * $value; }, 1);
// expected output: 3628800



$arrayUtils->reduceRight(
    function($accumulator, $value){
        echo  "$accumulator * $value = " . ($accumulator * $value) . PHP_EOL;
        return $accumulator * $value; }, 1));
//echo 1 * 10 = 10
//echo 10 * 9 = 90
//echo 90 * 8 = 720
//echo 720 * 7 = 5040
//echo 5040 * 6 = 30240
//echo 30240 * 5 = 151200
//echo 151200 * 4 = 604800
//echo 604800 * 3 = 1814400
//echo 1814400 * 2 = 3628800
//echo 3628800 * 1 = 3628800
//expected output: 3628800
```

{% endcode %}

## Syntax

```php
$arrayUtils->reduceRight(callable $callback, mixed $initialValue = null) : mixed;
```

### Parameter

* `$callback`

  > A function that produces an element of the new Array, taking four arguments:
  >
  > * `$accumulator`The accumulator accumulates callback's return values. It is the accumulated value previously returned in the last invocation of the callback—or `$initialValue`, if it was supplied (see below).
  > * `$value` The current element being processed in the array.
  > * `$key` The index of the current element being processed in the array.
  > * `$array`  The array `every` was called upon.
* `$initialValue` <img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;
  * A value to use as the first argument to the first call of the callback.

### Return value

* The result value.

## Prefixing

```php
ArrayUtils::reduceRightFrom(iterable $from, callable $callback, mixed $initialValue = null) : mixed;
```

## References

{% content-ref url="/pages/-MKmSpThy4vgovSVrqFM" %}
[reduce()](/methods/g/reduce)
{% endcontent-ref %}

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/ReduceRight>" %}


# sum()

The sum() method calculate the sum of values in an array

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

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

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

$arrayUtils->sum();// expected output: 55
```

{% endcode %}

## Syntax

```php
$arrayUtils->sum() : int|float;
```

### Return value

* &#x20;The sum of values as an integer or float

## Prefixing

```php
ArrayUtils::sumFrom(iterable $from) : int|float;
```

## References

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


# pop()

The pop() method removes the last element and returns that element

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

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

$arrayUtils = ArrayUtils::from(["Apple", "Banana", "Carrot"]);

//Gets last value
$arrayUtils->pop();
// expected output: "Carrot"

$arrayUtils;
// expected output: ["Apple", "Banana"]
```

{% endcode %}

## Syntax

```php
$arrayUtils->pop() : mixed;
```

### Return value

* The last value of array.

## Prefixing

```php
ArrayUtils::pop() : mixed;
```

## References

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

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop>" %}


# shift()

The shift() method removes the first element and returns that element

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

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

ArrayUtils::from(range(1, 20))->chunk(4);
//[
//  [ 1,  2,  3,  4],
//  [ 5,  6,  7,  8],
//  [ 9, 10, 11, 12],
//  [13, 14, 15, 16],
//  [17, 18, 19, 20]
//]

ArrayUtils::from(range(1, 20))->chunk(4, true);
//[
//  [ 0 =>  1,  1 =>  2,  2 =>  3,  3 =>  4],
//  [ 4 =>  5,  5 =>  6,  6 =>  7,  7 =>  8],
//  [ 8 =>  9,  9 => 10, 10 => 11, 11 => 12],
//  [12 => 13, 13 => 14, 14 => 15, 15 => 16],
//  [16 => 17, 17 => 18, 18 => 19, 19 => 20]
//]
```

{% endcode %}

## Syntax

```php
$arrayUtils->shift() : mixed;
```

### Return value

* The first value of array.

## Prefixing

```php
ArrayUtils::shift() : mixed;
```

## References

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

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift>" %}


# includes()

The includes() method tests whether an array includes a certain value among its entries

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

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

$arrayUtils = ArrayUtils::from(["Apple", "Banana", "Carrot"]);

//Check array includes "Banana"
$arrayUtils->includes("Banana");
// expected output: true

//Check array includes "Banana" from 2
$arrayUtils->includes("Banana", 2);
// expected output: false

//Check array includes "Baccon"
$arrayUtils->includes("Baccon");
// expected output: false
```

{% endcode %}

## Syntax

```php
$arrayUtils->includes(mixed $needle, int $start = 0) : bool;
```

### Parameter

* `$needle`
  * The value to search for.
* `$start` <img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;
  * &#x20;The position in this array at which to begin searching for `valueToFind`.
  * &#x20;Defaults to `0`.

### Return value

* A `boolean` the whether the element exists in the array

## Prefixing

```php
ArrayUtils::includesFrom(iterable $from, mixed $needle, int $start = 0) : bool;
```

## References

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

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes>" %}


# keyExists()

The keyExists() method tests whether the requested index exists.

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

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

$arrayUtils = ArrayUtils::from(["Apple", "Banana", "Carrot", "FAV" => "Becon"]);

//Check array has each indexs
$arrayUtils->keyExists(1);      // expected output: true
$arrayUtils->keyExists(2);      // expected output: true
$arrayUtils->keyExists(3);      // expected output: false
$arrayUtils->keyExists("HATE"); // expected output: false
$arrayUtils->keyExists("FAV");  // expected output: true
```

{% endcode %}

## Syntax

```php
$arrayUtils->keyExists(int|string $key) : bool;
```

### Parameter

* `$key`

  > The index being checked.

### Return value

* `Boolean` the whether the requested index exists

## Prefixing

```php
ArrayUtils::keyExistsFrom(iterable $from, int|string $key) : bool;
```

## References

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

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


# indexOf()

The indexOf() method get first index at which a given element can be found in the array

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

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

$arrayUtils = ArrayUtils::from(["Apple", "Banana", "Carrot"]);

//Check array includes "Banana"
$arrayUtils->indexOf("Banana");
// expected output: 1

//Check array includes "Carrot" from 2
$arrayUtils->indexOf("Banana");
// expected output: null

//Check array includes "Baccon"
$arrayUtils->indexOf("Baccon");
// expected output: null
```

{% endcode %}

## Syntax

```php
$arrayUtils->indexOf(mixed $needle, int $start = 0) : int|string|null;
```

### Parameter

* `$needle`
  * The value to search for.
* `$start` <img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;

  * &#x20;The position in this array at which to begin searching for `valueToFind`.
  * &#x20;Defaults to `0`.

### Return value

* The  first index of the element in the array. If not founded, returns `NULL`.

## Prefixing

```php
ArrayUtils::indexOfFrom(iterable $from, mixed $needle, int $start = 0) : int|string|null;
```

## References

<https://www.php.net/manual/en/function.array-chunk>


# find()

The find() method find the value of the first element that pass function.

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

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

$arrayUtils = ArrayUtils::from(["Apple", "Banana", "Carrot", "Bacon"]);

//Find values ​​starting with B
$arrayUtils->find(function($name){ return $name[0] === "B"; });
// expected output: "Banana"
```

{% endcode %}

## Syntax

```php
$arrayUtils->find(callable $callback) : mixed;
```

### Parameter

* `$callback`

  > A function to execute on each value in the array, taking 3 arguments:
  >
  > * `$value` The current element being processed in the array.
  > * `$key` The index of the current element being processed in the array.
  > * `$array`  The array `every` was called upon.

###

### Return value

* &#x20;The **value** of the **first element** in the array that pass function.\
  Otherwise, `NULL` is returned.

## Prefixing

```php
ArrayUtils::findFrom(iterable $from, callable $callback) : mixed;
```

## References

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find>" %}


# findIndex()

The findIndex() method all similar to find(), but return index

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

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

$arrayUtils = ArrayUtils::from(["Apple", "Banana", "Carrot", "Bacon"]);

//Find value's index ​​starting with B
$arrayUtils->findIndex(function($name){ return $name[0] === "B"; });
// expected output: 1
```

{% endcode %}

## Syntax

```php
$arrayUtils->find(callable $callback) : mixed;
```

### Parameter

* `$callback`

  > A function to execute on each value in the array, taking 3 arguments:
  >
  > * `$value` The current element being processed in the array.
  > * `$key` The index of the current element being processed in the array.
  > * `$array`  The array `every` was called upon.

### Return value

* The **key** of the **first element** in the array that pass function.\
  Otherwise, `NULL` is returned.

## Prefixing

```php
ArrayUtils::findFrom(iterable $from, callable $callback) : mixed;
```

## References

<https://www.php.net/manual/en/function.array-chunk>


# first()

The first() method returns first value

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

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

$arrayUtils = ArrayUtils::from(["Apple", "Banana", "Carrot"]);

//Gets first value
$arrayUtils->first();
// expected output: "Apple"
```

{% endcode %}

## Syntax

```php
$arrayUtils->first() : mixed;
```

### Return value

* A first value of array. If array is empty, returns `NULL`.

## Prefixing

```php
ArrayUtils::firstFrom(iterable $from) : mixed;
```


# keyFirst()

The keyFirst() method returns first key

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

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

$arrayUtils = ArrayUtils::from(["Apple" => 0, "Banana" => 1, "Carrot" => 2]);

//Gets first key
$arrayUtils->keyFirst();
// expected output: "Apple"
```

{% endcode %}

## Syntax

```php
$arrayUtils->keyFirst() : int|string|null;
```

### Return value

* A first key of array. If array is empty, returns `NULL`.

## Prefixing

```php
ArrayUtils::keyFirstFrom(iterable $from) : int|string|null;
```

## References

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


# last()

The last() method returns last value

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

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

$arrayUtils = ArrayUtils::from(["Apple", "Banana", "Carrot"]);

//Gets last value
$arrayUtils->last();
// expected output: "Carrot"
```

{% endcode %}

## Syntax

```php
$arrayUtils->last() : mixed;
```

### Return value

* A last value of array. If array is empty, returns `NULL`.

## Prefixing

```php
ArrayUtils::lastFrom(iterable $from) : mixed;
```


# keyLast()

The keyLast() method returns last key

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

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

$arrayUtils = ArrayUtils::from(["Apple" => 0, "Banana" => 1, "Carrot" => 2]);

//Gets last key
$arrayUtils->keyLast();
// expected output: "Carrot"
```

{% endcode %}

## Syntax

```php
$arrayUtils->keyLast() : int|string|null;
```

### Return value

* A last key of array. If array is empty, returns `NULL`.

## Prefixing

```php
ArrayUtils::keyLastFrom(iterable $from) : int|string|null;
```

## References

[https://www.php.net/manual/en/function.array-key-last](https://www.php.net/manual/en/function.array-key-last.php)


# random()

The random() method returns random value

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

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

$arrayUtils = ArrayUtils::from(["Apple", "Banana", "Carrot"]);

//Gets random value
$arrayUtils->random();
// output is random, so always different
```

{% endcode %}

## Syntax

```php
$arrayUtils->random() : mixed;
```

### Return value

* A random value from array. If array is empty, returns `NULL`.

## Prefixing

```php
ArrayUtils::randomFrom(iterable $from) : mixed;
```


# keyRandom()

The keyRandom() method returns random key

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

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

$arrayUtils = ArrayUtils::from(["Apple" => 0, "Banana" => 1, "Carrot" => 2]);

//Gets random key
$arrayUtils->keyRandom();
// output is random, so always different
```

{% endcode %}

## Syntax

```php
$arrayUtils->keyRandom() : int|string|null;
```

### Return value

* A random key from array. If array is empty, returns `NULL`.

## Prefixing

```php
ArrayUtils::keyRandomFrom(iterable $from) : int|string|null;
```

## References

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


# splice()

The splice() method remove a portion of the array and replace it with something else

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

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

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

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


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

$arrayUtils->splice(2, 4, "Hi", "Bye", "Oh");
// expected output: [3, 4, 5, 6]
$arrayUtils->splice(-4);
// expected output: [1, 2, "Hi", "Bye", "Oh", 7, 8, 9]
```

{% endcode %}

## Syntax

```php
$arrayUtils->splice(int $offset, ?int $length = null, mixed ...$replacement) : ArrayUtils;
```

### Parameter

* `$offset`

  > The index at which to start changing the array.
* `$length`<img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;

  > Length of array to be removed
  >
  > Default is `NULL`. If is null, It replaced to `count($array)-$offest`.
* `$replacement`<img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;

  > Values to replace.\
  > If if empty, Removes elements in the selected range.

### Return value

* The array consisting of the extracted elements.

## Prefixing

```php
$arrayUtils->spliceAs(int $offset, ?int $length = null, mixed ...$replacement) : array;
```

```php
ArrayUtils::spliceFrom(iterable $from, int $offset, ?int $length = null, mixed ...$replacement) : ArrayUtils;
```

```php
ArrayUtils::spliceFromAs(iterable $from, int $offset, ?int $length = null, mixed ...$replacement) : array;
```

## References

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

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice>" %}


# ⚡Chain method

The chaining-methods is member methods that returns an ArrayUtils instance.

> This chaining-method returns an `ArrayUtils` instance.\
> Because of this, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results.
>
> > For a detailed description of the method chaining method, [click here](https://en.wikipedia.org/wiki/Method_chaining)

## :bookmark:Support prefix

{% content-ref url="/pages/-MKpIDQbxjHxelU6Oe9-" %}
[Suffix - From](/prefixs/from)
{% endcontent-ref %}

{% content-ref url="/pages/-MKpMOnqm75Parhz3L6k" %}
[Suffix - As](/prefixs/as)
{% endcontent-ref %}

{% hint style="success" %}
You can use two prefix at once.

```php
var_export(ArrayUtils::reverseFromAs([1,2,3,4,5]));
//array (0 => 5, 1 => 4, 2 => 3, 3 => 2, 4 => 1)
```

{% endhint %}

## :bookmark:Method list

{% content-ref url="/pages/-MKmay\_SQS46AXw\_kFD6" %}
[chunk()](/methods/c/chunk)
{% endcontent-ref %}

{% content-ref url="/pages/-MKjzUNeNDWKiPjgukhz" %}
[column()](/methods/c/column)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpSy5TL6Z3U1gzI4" %}
[combine()](/methods/c/combine)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpSzuG0Vafxf37nw" %}
[concat()](/methods/c/concat)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpT-MbJgEp\_S6YY8" %}
[concatSoft()](/methods/c/concat/soft)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpT0tMTp1o-0qSyj" %}
[countValues()](/methods/c/count-values)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpT1wyYa\_hpoCqTo" %}
[diff()](/methods/c/diff)
{% endcontent-ref %}

{% content-ref url="/pages/-MKnPvl55KtXkWU9e6-j" %}
[diffAssoc()](/methods/c/diff/assoc)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpT2EUAilE3iQ5Xa" %}
[diffKey()](/methods/c/diff/key)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpT3hDysGmMglKjn" %}
[fill()](/methods/c/fill)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpT4P7WXYqMI\_Z8G" %}
[fillKeys()](/methods/c/fill/keys)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpT6gSULomuRm6rz" %}
[filter()](/methods/c/filter)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpT5TfSG7godmCVM" %}
[flat()](/methods/c/flat)
{% endcontent-ref %}

{% content-ref url="/pages/-MKnPvlCGDmctUtRguAX" %}
[flatMap()](/methods/c/flat/map)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpT81\_7Quiwfb2Ji" %}
[forEach()](/methods/c/for-each)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpT9GFt-g4wslUOY" %}
[intersect()](/methods/c/intersect)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTAYhR\_MyjnN3lx" %}
[intersectAssoc()](/methods/c/intersect/assoc)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTBkO68BWZEmgUB" %}
[intersectKey()](/methods/c/intersect/key)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTCw335q5Hde2qr" %}
[keys()](/methods/c/keys)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTD\_xJWCBfX5Mod" %}
[map()](/methods/c/map)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTEa84eFGu2BSdW" %}
[mapAssoc()](/methods/c/map/assoc)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTFnRc7U\_iP3saa" %}
[mapKey()](/methods/c/map/key)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTIz-PNZRIPveqv" %}
[pad()](/methods/c/pad)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTJX9Uo5R5MdlG4" %}
[push()](/methods/c/push)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTKxNfH2fakE1tJ" %}
[replace()](/methods/c/replace)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTL7mssNHZbvBOE" %}
[reverse()](/methods/c/reverse)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTMAlBppF6Uhu33" %}
[slice()](/methods/c/slice)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTeK5WXHIFdCWNK" %}
[splice()](/methods/g/splice)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTNdMGbyxqchPMQ" %}
[sort()](/methods/c/sort)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTOCoYKAPKb56Wt" %}
[sortKey()](/methods/c/sort/key)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTQMy2aTv5flnGH" %}
[unique()](/methods/c/unique)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTRWnsOxDvP-Ik9" %}
[unshift()](/methods/c/unshift)
{% endcontent-ref %}

{% content-ref url="/pages/-MKmSpTSGfx2yHbFtrYI" %}
[values()](/methods/c/values)
{% endcontent-ref %}


# chunk()

The chunk() method split an array into chunks

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

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

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

$arrayUtils->chunk(4);
// expected output: [
//   [ 1,  2,  3,  4],
//   [ 5,  6,  7,  8],
//   [ 9, 10, 11, 12],
//   [13, 14, 15, 16],
//   [17, 18, 19, 20]
//]

$arrayUtils->chunk(4, true);
// expected output: [
//   [ 0 =>  1,  1 =>  2,  2 =>  3,  3 =>  4],
//   [ 4 =>  5,  5 =>  6,  6 =>  7,  7 =>  8],
//   [ 8 =>  9,  9 => 10, 10 => 11, 11 => 12],
//   [12 => 13, 13 => 14, 14 => 15, 15 => 16],
//   [16 => 17, 17 => 18, 18 => 19, 19 => 20]
//]
```

{% endcode %}

## Syntax

```php
$arrayUtils->chunk(int $size, bool $preserveKeys = FALSE) : ArrayUtils;
```

### Parameter

* `$size`

  > The size of each chunk
* `$preserveKeys`  <img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" 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 multidimensional numerically indexed array, starting with zero, with each dimension containing `size` elements.

## Prefixing

```php
$arrayUtils->chunkAs(int $size, bool $preserveKeys = FALSE) : array;
```

```php
ArrayUtils::chunkFrom(iterable $from, int $size, bool $preserveKeys = FALSE) : ArrayUtils;
```

```php
ArrayUtils::chunkFromAs(iterable $from, int $size, bool $preserveKeys = FALSE) : array;
```

## References

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


# column()

The column() method returns the values from a single column in the input array

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

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

$arrayUtils = ArrayUtils::from([
    ["any" => "first",  1, 2, 3],
    ["any" => "second", 4, 5, 6],
    ["any" => "third",  7, 8, 9]
]);

// Use "any" value in internal array as value of array
$arrayUtils->column("any");
// expected output: ["first", "second", "third"]


// Use 2nd value in internal array as value,
// Use "any" in internal array as key of array
$arrayUtils->column(1, "any");
// expected output: ["first" => 2, "second" => 5, "third" => 8]


// Use value in internal array as value,
// Use "any" in internal array as key of array
$arrayUtils->column(null, "any");
// expected output: [
//   "first"  => ["any" => "first",  1, 2, 3],
//   "second" => ["any" => "second", 4, 5, 6],
//   "third"  => ["any" => "third",  7, 8, 9]
//]
```

{% endcode %}

## Syntax

```php
$arrayUtils->column(mixed $valueKey, mixed $indexKey = null) : ArrayUtils;
```

### Parameter

* `$valueKey`

  > The key value of the element to be used as the value.
  >
  > If is null, Use element to value.<br>
* `$indexKey` <img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;

  > The key value of the element to be used as the key.\
  > Default is `NULL`. If is null, Re-index from 0.

### Return value

* A array of values representing a single column from the input array.

## Prefixing

```php
$arrayUtils->columnAs(mixed $valueKey, mixed $indexKey = null) : array;
```

```php
ArrayUtils::columnFrom(iterable $from, mixed $valueKey, mixed $indexKey = null) : ArrayUtils;
```

```php
ArrayUtils::(iterable $from, mixed $valueKey, mixed $indexKey = null) : array;
```

## References

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


# combine()

The combine() method returns new array by using one array for keys and another for values

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

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

$arrayUtils = ArrayUtils::from(["first", "second", "third"]);

//General usage
$arrayUtils->combine([1, 2, 3]);
// expected output: ["first" => 1, "second" => 2, "third" => 3]

//Combine itself
$arrayUtils->combine();
// expected output: [
//   "first" => "first", 
//   "second" => "second", 
//   "third" => "third"
//]
```

{% endcode %}

## Syntax

```php
$arrayUtils->combine(iterable|null $valueArray = null) : ArrayUtils;
```

### Parameter

* `$valueArray` <img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;

  > An array of elements to use as values.\
  > Default is `NULL`. If is null, Use itself.

### Return value

* &#x20;A combined array.

{% hint style="danger" %}
If the number of elements for each array isn't equal, It will be throw error
{% endhint %}

## Prefixing

```php
$arrayUtils->combineAs(iterable|null $valueArray = null) : array;
```

```php
ArrayUtils::combineFrom(iterable $from, iterable|null $valueArray = null) : ArrayUtils;
```

```php
ArrayUtils::combineFromAs(iterable $from, iterable|null $valueArray = null) : array;
```

## References

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


# concat()

The concat() method merge one or more arrays

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

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

$arrayUtils = ArrayUtils::from(["first" => 1, "second" => 2, "third" => 3]);

//Same key values ​​are overwritten
$arrayUtils->concat(["first" => 0, "4th" => 4]);
// expected output: ["first" => 0, "second" => 2, "third" => 3, "4th" => 4]

//Non-array values ​​can also be combined
$arrayUtils->concat(4, 5, 6);
// expected output: ["first" => 1, "second" => 2, "third" => 3, 4, 5, 6]
```

{% endcode %}

## Syntax

```php
$arrayUtils->concat(mixed ...$values) : ArrayUtils;
```

### Parameter

* `$values`

  > A new array or value to merge with the existing array.\
  > If is not array (Immutable into an array), It will be wrapped in array.

### Return value

* A merged array. The same key values ​​are overwritten.

## Prefixing

```php
$arrayUtils->concatAs(mixed ...$values) : array;
```

```php
ArrayUtils::concatFrom(mixed ...$values) : ArrayUtils;
```

```php
ArrayUtils::concatFromAs(mixed ...$values) : array;
```

## References

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat>" %}

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


# concatSoft()

The concatSoft() method all similar to concat(), but not overwrite existing keys

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

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

$arrayUtils = ArrayUtils::from(["first" => 1, "second" => 2, "third" => 3]);

//Same key values are ignored
$arrayUtils->concat(["first" => 0, "4th" => 4]);
// expected output: ["first" => 1, "second" => 2, "third" => 3, "4th" => 4]

//Non-array values ​​can also be combined
$arrayUtils->concat(4, 5, 6);
// expected output: ["first" => 1, "second" => 2, "third" => 3, 4, 5, 6]
```

{% endcode %}

## Syntax

```php
$arrayUtils->concatSoft(mixed ...$values) : ArrayUtils;
```

### Parameter

* `$values`

  > A new array or value to merge with the existing array.\
  > If is not array (Immutable into an array), It will be wrapped in array.

  **Return value**

  * A merged array. The same key values ​​are ignored.

## Prefixing

```php
$arrayUtils->concatSoftAs(mixed ...$values) : array;
```

```php
ArrayUtils::concatSoftFrom(mixed ...$values) : ArrayUtils;
```

```php
ArrayUtils::chunkFromAsconcatSoftFromAs(mixed ...$values) : array;
```

## References

{% content-ref url="/pages/-MKmSpSzuG0Vafxf37nw" %}
[concat()](/methods/c/concat)
{% endcontent-ref %}


# countValues()

The countValues() method counts all the values of an array

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

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

$arrayUtils = ArrayUtils::from(["a", "a", "a", "b", "c", "c", "d"]);

//General usage
$arrayUtils->countValues();
// expected output: ["a" => 3, "b" => 1, "c" => 2, "d" => 1]
```

{% endcode %}

## Syntax

```php
$arrayUtils->countValues() : ArrayUtils;
```

### Return value

* A associative array of values from array as keys and their count as value.

## Polymorphism

```php
$arrayUtils->countValuesAs() : array;
```

```php
ArrayUtils::countValuesFrom(iterable $from) : ArrayUtils;
```

```php
ArrayUtils::countValuesFromAs(iterable $from) : array;
```

## References

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


# diff()

The diff() method compares with other arrays and returns the values that are not in any of the other arrays

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

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

$arrayUtils = ArrayUtils::from(["first", "second", "third"]);

//General array comparison
$arrayUtils->diff(["first", "4th"]);
// expected output: ["second", "third"]
```

{% endcode %}

## Syntax

```php
$arrayUtils->diff(iterable ...$iterables) : ArrayUtils;
```

### Parameter

* `$iterables`

  > Arrays to compare.

### Return value

* A array containing all the entries that are not present in any of the other arrays. (Keys are preserved)

## Prefixing

```php
$arrayUtils->diffAs(iterable ...$iterables) : array;
```

```php
ArrayUtils::diffFrom(iterable $from, iterable ...$iterables) : ArrayUtils;
```

```php
ArrayUtils::diffFromAs(iterable $from, iterable ...$iterables) : array;
```

## References

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


# diffAssoc()

The diffAssoc() method all similar to diff(), but this applies with additional index check

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

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

$arrayUtils = ArrayUtils::from(["first" => 1, "second" => 2, "third" => 3]);

//General array comparison
$arrayUtils->diffAssoc(["first" => 404, "second" => 2]);
// expected output: ["first" => 1, "third" => 3]
```

{% endcode %}

## Syntax

```php
$arrayUtils->diffAssoc(iterable ...$iterables) : ArrayUtils;
```

### Parameter

* `$iterables`

  > Arrays to compare.

### Return value

* A array containing all the entries that are not present in any of the other arrays. (Keys are preserved)

## Prefixing

```php
$arrayUtils->diffAssocAs(iterable ...$iterables) : array;
```

```php
ArrayUtils::diffAssocFrom(iterable $from, iterable ...$iterables) : ArrayUtils;
```

```php
ArrayUtils::diffAssocFromAs(iterable $from, iterable ...$iterables) : array;
```

## References

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

{% content-ref url="/pages/-MKmSpT1wyYa\_hpoCqTo" %}
[diff()](/methods/c/diff)
{% endcontent-ref %}


# diffKey()

The diffAssoc() method all similar to diff(), but this applies to keys

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

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

$arrayUtils = ArrayUtils::from(["first" => 1, "second" => 2, "third" => 3]);

//General array comparison
$arrayUtils->diffKey(["first" => 404, "second" => 404]);
// expected output: ["third" => 3]
```

{% endcode %}

## Syntax

```php
$arrayUtils->diffKey(iterable ...$iterables) : ArrayUtils;
```

### Parameter

* `$iterables`

  > Arrays to compare.

### Return value

* A array containing all the entries that are not present in any of the other arrays. (Keys are preserved)

###

## Prefixing

```php
$arrayUtils->diffKeyAs(iterable ...$iterables) : array;
```

```php
ArrayUtils::diffKeyFrom(iterable $from, iterable ...$iterables) : ArrayUtils;
```

```php
ArrayUtils::diffKeyFromAs(iterable $from, iterable ...$iterables) : array;
```

## References

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

{% content-ref url="/pages/-MKmSpT1wyYa\_hpoCqTo" %}
[diff()](/methods/c/diff)
{% endcontent-ref %}


# fill()

The fill() method changes all values to a static value, from a start index to an end index

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

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

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

//Full fill with 0
$arrayUtils->fill(0);
// expected output: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

//Fill with 0 from position 2 until position 4
$arrayUtils->fill(0, 2, 4);
// expected output: [1, 2, 0, 0, 5, 6, 7, 8, 9, 10]

//Fill with 0 from position 4 until end
$arrayUtils->fill(0, 4);
// expected output: [1, 2, 3, 4, 0, 0, 0, 0, 0, 0]
```

{% endcode %}

## Syntax

```php
$arrayUtils->fill(mixed $value, int $start = 0, int $end = null) : ArrayUtils;
```

### Parameter

* `$value`&#x20;

  > Value to fill the array with.
* `$start`<img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;

  > Start index, default `0`.
* `$end` <img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;

  > End index, default `count($array)`.

### Return value

* A filled array.

## Prefixing

```php
$arrayUtils->fillAs(mixed $value, int $start = 0, int $end = null) : array;
```

```php
ArrayUtils::fillFrom(iterable $from, mixed $value, int $start = 0, int $end = null) : ArrayUtils;
```

```php
ArrayUtils::fillFromAs(iterable $from, mixed $value, int $start = 0, int $end = null) : array;
```

## References

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


# fillKeys()

The fillKeys() method fills an array with the static value, using the values of the array as keys.

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

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

$arrayUtils = ArrayUtils::from(["first", "second", "third"]);

//Fill keys with 0
$arrayUtils->fillKeys(0);
// expected output: ["first" => 0, "second" => 0, "third" => 0]
```

{% endcode %}

## Syntax

```php
$arrayUtils->fillKeys(mixed $value) : ArrayUtils;
```

### Parameter

* `$value`&#x20;

  > Value to fill the array with.

### Return value

* A filled array.

## Prefixing

```php
$arrayUtils->fillKeysAs(mixed $value) : array;
```

```php
ArrayUtils::fillKeysFrom(iterable $from, mixed $value) : ArrayUtils;
```

```php
ArrayUtils::fillKeysFromAs(iterable $from, mixed $value) : array;
```

## References

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


# filter()

The filter() method creates a new array with all elements that pass the function.

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

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

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

//Filtering values ​​between 3 and 6
$arrayUtils->filter(function($num){ return $num < 3 || $num > 6; });
// expected output: [1, 2, 7, 8, 9, 10]
```

{% endcode %}

## Syntax

```php
$arrayUtils->filter(callable $callback) : ArrayUtils;
```

### Parameter

* `$callback`

  > A function to test each element of the array.\
  > Return a value that coerces to `TRUE` to keep the element, or to `FALSE` otherwise.\
  > Function taking three arguments:
  >
  > * `$value` The current element being processed in the array.
  > * `$key` The index of the current element being processed in the array.
  > * `$array`  The array `every` was called upon.

### Return value

* A filtered array.

## Prefixing

```php
$arrayUtils->filterAs(callable $callback) : array;
```

```php
ArrayUtils::filterFrom(iterable $from, callable $callback) : ArrayUtils;
```

```php
ArrayUtils::filterFromAs(iterable $from, callable $callback) : array;
```

## References

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

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter>" %}


# flat()

The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

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

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

$arrayUtils = ArrayUtils::from([
    [1, 2, 3],
    [[4, 5, 6], [7, 8, 9]],
    10
]);

//To flat single level array
$arrayUtils->flat();
// expected output: [1, 2, 3, [4, 5, 6], [7, 8, 9], 10]

//To flat two level array
$arrayUtils->flat(2);
// expected output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
```

{% endcode %}

## Syntax

```php
$arrayUtils->flat(int $dept = 1) : ArrayUtils;
```

### Parameter

* `$dept` <img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;
  * The depth level that should be flattened.\
    Default is `1`.

### Return value

* A new array with the sub-array elements concatenated into it.

## Prefixing

```php
$arrayUtils->flatAs(int $dept = 1) : array;
```

```php
ArrayUtils::flatFrom(iterable $from, int $dept = 1) : ArrayUtils;
```

```php
ArrayUtils::flatFromAs(iterable $from, int $dept = 1) : array;
```

## References

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat>" %}


# flatMap()

The flatMap() method create a new array formed by applying function and then flattening the result by one level

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

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

$arrayUtils = ArrayUtils::from([-3, 2, -4, 5, 9]);

//Element duplication
$arrayUtils->flatMap(function($num){ return [$num, $num]; });
// expected output: [-3, -3, 2, 2, -4, -4, 5, 5, 9, 9]

//Remove negative and split the odd numbers into an even number and a 1
$arrayUtils->flatMap(function($num){
  if($num< 0)       return [];
  if($num % 2 == 0) return [$num];
  else              return [$num - 1, 1]; });
// expected output: [2, 4, 1, 8, 1]
```

{% endcode %}

## Syntax

```php
$arrayUtils->flatMap(callable $callback) : ArrayUtils;
```

### Parameter

* `$callback`

  > A function that produces an element of the new Array, taking three arguments:
  >
  > * `$value` The current element being processed in the array.
  > * `$key` The index of the current element being processed in the array.
  > * `$array`  The array `every` was called upon.

###

### Return value

* A new array with each element being the result of the callback function and flattened to a depth of 1.

## Prefixing

```php
$arrayUtils->flatMapAs(callable $callback) : array;
```

```php
ArrayUtils::flatMapFrom(iterable $from, callable $callback) : ArrayUtils;
```

```php
ArrayUtils::flatMapFromAs(iterable $from, callable $callback) : array;
```

## References

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap>" %}


# flip()

The flip() method exchanges all keys with their values in an array

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

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

$arrayUtils = ArrayUtils::from(["a", "b", "c", "d"]);

//General usage
$arrayUtils->flip();
// expected output: ["a" => 0, "b" => 1, "c" => 2, "d" => 3]
```

{% endcode %}

## Syntax

```php
$arrayUtils->flip() : ArrayUtils;
```

### Return value

* A flipped array on success and **`NULL`** on failure.

## Prefixing

```php
$arrayUtils->flipAs() : array;
```

```php
ArrayUtils::flipFrom(iterable $from) : ArrayUtils;
```

```php
ArrayUtils::flipFromAs(iterable $from) : array;
```

## References

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


# forEach()

The forEach() method executes a provided function once for each array element.

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

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

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

//Filtering values ​​between 3 and 6
$arrayUtils->forEach(function($num){ echo $num . "~"; });
// expected output: 1~2~3~4~5~6~7~8~9~10~
```

{% endcode %}

## Syntax

```php
$arrayUtils->forEach(callable $callback) : ArrayUtils;
```

### Parameter

* `$callback`

  > A function to execute on each element, taking three arguments:
  >
  > * `$value` The current element being processed in the array.
  > * `$key` The index of the current element being processed in the array.
  > * `$array`  The array `every` was called upon.

### Return value

* oneself back for method chaining.

## Prefixing

```php
$arrayUtils->forEachAs(callable $callback) : array;
```

```php
ArrayUtils::forEachFrom(iterable $from, callable $callback) : ArrayUtils;
```

```php
ArrayUtils::forEachFromAs(iterable $from, callable $callback) : array;
```

## References

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach>" %}


# intersect()

The intersect() method computes the intersection with other arrays and returns the values that intersects with another array.

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

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

$arrayUtils = ArrayUtils::from(["first", "second", "third"]);

//General array comparison
$arrayUtils->intersect(["first", "third"]);
// expected output: ["first", "third"]
```

{% endcode %}

## Syntax

```php
$arrayUtils->intersect(iterable ...$iterables) : ArrayUtils;
```

### Parameter

* `$iterables`

  > Arrays to compare.

### Return value

* A array containing all of the values that intersects with another array.

## Prefixing

```php
$arrayUtils->intersectAs(iterable ...$iterables) : array;
```

```php
ArrayUtils::intersectFrom(iterable $from, iterable ...$iterables) : ArrayUtils;
```

```php
ArrayUtils::intersectFromAs(iterable $from, iterable ...$iterables) : array;
```

## References

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


# intersectAssoc()

The intersectAssoc() method all similar to intersect(), but this applies with additional index check

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

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

$arrayUtils = ArrayUtils::from(["first" => 1, "second" => 2, "third" => 3]);

//General array comparison
$arrayUtils->intersectAssoc(["first" => 404, "second" => 2]);
// expected output: ["second" => 2]
```

{% endcode %}

## Syntax

```php
$arrayUtils->intersectAssoc(iterable ...$iterables) : ArrayUtils;
```

### Parameter

* `$iterables`

  > Arrays to compare.

### Return value

* A array containing all of the values that intersects with another array.

## Prefixing

```php
$arrayUtils->intersectAssocAs(iterable ...$iterables) : array;
```

```php
ArrayUtils::intersectAssocFrom(iterable $from, iterable ...$iterables) : ArrayUtils;
```

```php
ArrayUtils::intersectAssocFromAs(iterable $from, iterable ...$iterables) : array;
```

## References

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


# intersectKey()

The intersectKey() method all similar to intersect(), but this applies to keys

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

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

$arrayUtils = ArrayUtils::from(["first" => 1, "second" => 2, "third" => 3]);

//General array comparison
$arrayUtils->intersectKey(["first" => 404, "second" => 2]);
// expected output: ["first" => 1, "second" => 2]
```

{% endcode %}

## Syntax

```php
$arrayUtils->intersectkey(iterable ...$iterables) : ArrayUtils;
```

### Parameter

* `$iterables`

  > Arrays to compare.

### Return value

* A array containing all of the values that intersects with another array.

## Prefixing

```php
$arrayUtils->intersectkeyAs(iterable ...$iterables) : array;
```

```php
ArrayUtils::intersectkeyFrom(iterable $from, iterable ...$iterables) : ArrayUtils;
```

```php
ArrayUtils::intersectkeyFromAs(iterable $from, iterable ...$iterables) : array;
```

## References

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


# keys()

The keys() method return all the keys of an array

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

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

$arrayUtils = ArrayUtils::from(["first" => 1, "second" => 2, "third" => 3]);

//Get all keys
$arrayUtils->keys();
// expected output: ["first", "second", "third"]
```

{% endcode %}

## Syntax

```php
$arrayUtils->keys() : ArrayUtils;
```

### Return value

* &#x20;A array of all the keys in `array`.

## Prefixing

```php
$arrayUtils->keysAs() : array;
```

```php
ArrayUtils::keysFrom(iterable $from) : ArrayUtils;
```

```php
ArrayUtils::keysFrom(iterable $from) : array;
```

## References

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

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys>" %}


# map()

The map() method applies the callback to the elements

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

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

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

//Squared all elements
$arrayUtils->map(function($num){ return $num * $num; });
// expected output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
```

{% endcode %}

## Syntax

```php
$arrayUtils->map(callable $callback) : ArrayUtils;
```

### Parameter

* `$callback`

  > A function that produces an element of the new Array, taking three arguments:
  >
  > * `$value` The current element being processed in the array.
  > * `$key` The index of the current element being processed in the array.
  > * `$array`  The array `every` was called upon.

###

### Return value

* A new array with each element being the result of the callback function.

## Prefixing

```php
$arrayUtils->mapAs(callable $callback) : array;
```

```php
ArrayUtils::mapFrom(iterable $from, callable $callback) : ArrayUtils;
```

```php
ArrayUtils::mapFromAs(iterable $from, callable $callback) : array;
```

## References

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

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map>" %}


# mapAssoc()

The mapAssoc() method all similar to map(), but this applies with additional index check

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

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

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

//Squared all elements
$arrayUtils->mapAssoc(function($num){ return ["$num * $num", $num * $num]; });
// expected output: [
//  "1 * 1"   => 1,
//  "2 * 2"   => 4,
//  "3 * 3"   => 9,
//  "4 * 4"   => 16,
//  "5 * 5"   => 25
//]
```

{% endcode %}

## Syntax

```php
$arrayUtils->mapAssoc(callable $callback) : ArrayUtils;
```

### Parameter

* `$callback`

  > A function that produces an element of the new Array, taking three arguments:
  >
  > * `$value` The current element being processed in the array.
  > * `$key` The index of the current element being processed in the array.
  > * `$array`  The array `every` was called upon.

###

### Return value

* A new array with each element being the result of the callback function.

## Prefixing

```php
$arrayUtils->mapAssocAs(callable $callback) : array;
```

```php
ArrayUtils::mapAssocFrom(iterable $from, callable $callback) : ArrayUtils;
```

```php
ArrayUtils::mapAssocFromAs(iterable $from, callable $callback) : array;
```

## References

{% content-ref url="/pages/-MKmSpTD\_xJWCBfX5Mod" %}
[map()](/methods/c/map)
{% endcontent-ref %}


# mapKey()

The mapKey() method all similar to map(), but this applies to keys

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

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

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

//Squared all elements
$arrayUtils->mapKey(function($num){ return "$num * $num"; });
// expected output: [
//  "1 * 1"   => 1,
//  "2 * 2"   => 2,
//  "3 * 3"   => 3,
//  "4 * 4"   => 4,
//  "5 * 5"   => 5
//]
```

{% endcode %}

## Syntax

```php
$arrayUtils->mapKey(callable $callback) : ArrayUtils;
```

### Parameter

* `$callback`

  > A function that produces an element of the new Array, taking three arguments:
  >
  > * `$value` The current element being processed in the array.
  > * `$key` The index of the current element being processed in the array.
  > * `$array`  The array `every` was called upon.

###

### Return value

* A new array with each element being the result of the callback function.

## Prefixing

```php
$arrayUtils->mapKeyAs(callable $callback) : array;
```

```php
ArrayUtils::mapKeyFrom(iterable $from, callable $callback) : ArrayUtils;
```

```php
ArrayUtils::mapKeyFromAs(iterable $from, callable $callback) : array;
```

## References

{% content-ref url="/pages/-MKmSpTD\_xJWCBfX5Mod" %}
[map()](/methods/c/map)
{% endcontent-ref %}


# pad()

The pad() method pad array to the specified length with a value

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

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

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

//Fill with 0 to have 10 elements
$arrayUtils->pad(10, 0);
// expected output: [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]

//Fill with 0 to have 10 elements, append to front of array
$arrayUtils->pad(-10, 0);
// expected output: [0, 0, 0, 0, 0, 1, 2, 3, 4, 5]
```

{% endcode %}

## Syntax

```php
$arrayUtils->pad(int $size, mixed $value) : ArrayUtils;
```

### Parameter

* `$size`
  * New size of the array.
* `$value` &#x20;
  * &#x20;Value to pad if `array` is less than `size`.

### Return value

* A padded array.

## Prefixing

```php
$arrayUtils->padAs(int $size, mixed $value) : array;
```

```php
ArrayUtils::padFrom(iterable $from, int $size, mixed $value) : ArrayUtils;
```

```php
ArrayUtils::padFromAs(iterable $from, int $size, mixed $value) : array;
```

## References

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


# push()

The push() method push elements onto the end of array

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

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

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

//Push 0 and 10
$arrayUtils->push(0, 10);
// expected output: [1, 2, 3, 4, 5, 0, 10]
```

{% endcode %}

## Syntax

```php
$arrayUtils->push(mixed ...$values) : ArrayUtils;
```

### Parameter

* `$values`

  > A values to push into array

### Return value

* oneself back for method chaining.

## Polymorphism

```php
$arrayUtils->pushAs(mixed ...$values) : array;
```

```php
ArrayUtils::pushFrom(iterable $from, mixed ...$values) : ArrayUtils;
```

```php
ArrayUtils::pushFromAs(iterable $from, mixed ...$values) : array;
```

## References

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

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push>" %}


# replace()

The replace() method replaces elements from passed arrays into the first array

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

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

$arrayUtils = ArrayUtils::from(["orange", "banana", "apple", "raspberry", "kiwi"]);


$arrayUtils->replace([0 => "pineapple", 4 => "cherry"]);
// expected output: ["pineapple", "banana", "apple", "raspberry", "cherry"]
```

{% endcode %}

## Syntax

```php
$arrayUtils->replace(iterable ...$iterables) : ArrayUtils;
```

### Parameter

* `$iterables`

  > Arrays to replace.

###

### Return value

* A replaced array.

## Prefixing

```php
$arrayUtils->replaceAs(iterable ...$iterables) : array;
```

```php
ArrayUtils::replaceFrom(iterable $from, iterable ...$iterables) : ArrayUtils;
```

```php
ArrayUtils::replaceFromAs(iterable $from, iterable ...$iterables) : array;
```

## References

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


# reverse()

The reverse() method reverses an array

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

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

$arrayUtils = ArrayUtils::from(["orange", "banana", "apple", "raspberry", "kiwi"]);


$arrayUtils->reverse();
// expected output: ["kiwi", "raspberry", "apple", "banana", "orange"]
```

{% endcode %}

## Syntax

```php
$arrayUtils->reverse(bool $preserveKeys = false) : ArrayUtils;
```

### Parameter

* `$preserveKeys` <img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" 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 reversed array.

## Prefixing

```php
$arrayUtils->reverseAs(bool $preserveKeys = false) : array;
```

```php
ArrayUtils::reverseFrom(iterable $from, bool $preserveKeys = false) : ArrayUtils;
```

```php
ArrayUtils::reverseFromAs(iterable $from, bool $preserveKeys = false) : array;
```

## References

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


# slice()

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

{% 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="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;

  > Zero-based index at which to start extraction.
  >
  > Default is `0`.
* `$end` <img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;

  > Zero-based index at which to start extraction.
  >
  > Default is `count($array)`.
* `$preserveKeys` <img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" 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>" %}


# sort()

The sort() method sort an array by values using a function or default sort function

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

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

$arrayUtils = ArrayUtils::from(["orange", "banana", "apple", "raspberry", "kiwi"]);


$arrayUtils->sort();
// expected output: ["apple", "banana", "kiwi", "orange", "raspberry"]

$arrayUtils->sort(function($a, $b){ return strcmp($a, $b) * -1; })
// expected output: ["raspberry", "orange", "kiwi", "banana", "apple"]
```

{% endcode %}

## Syntax

```php
$arrayUtils->sort(?callable $callback = null) : ArrayUtils;
```

### Parameter

* `$callback` <img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;

  > A function to compare element for sort, taking two arguments:
  >
  > * `$a` The comparison target A
  > * `$b` The comparison target B
  >
  > Default is `NULL`, If is null, Sort by default sort function.

### Return value

* A sorted array.

## Prefixing

```php
$arrayUtils->sortAs(?callable $callback = null) : array;
```

```php
ArrayUtils::sortFrom(iterable $from, ?callable $callback = null) : ArrayUtils;
```

```php
ArrayUtils::sortFromAs(iterable $from, ?callable $callback = null) : array;
```

## References

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

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

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort>" %}


# sortKey()

The sortKey() method sort an array by keys using a function or default sort function

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

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

$arrayUtils = ArrayUtils::from([
    "orange" => 1, 
    "banana" => 2, 
    "apple" => 3,
    "raspberry" => 4, 
    "kiwi" => 5]);


$arrayUtils->sortKey();
// expected output: [
//  "apple" => 3,
//  "banana" => 2, 
//  "kiwi" => 5,
//  "orange" => 1, 
//  "raspberry" => 4
//]

$arrayUtils->sortKey(function($a, $b){ return strcmp($a, $b) * -1; })
// expected output: [
//  "raspberry" => 4,
//  "orange" => 1, 
//  "kiwi" => 5,
//  "banana" => 2, 
//  "apple" => 3
//]
```

{% endcode %}

## Syntax

```php
$arrayUtils->sortKey(?callable $callback = null) : ArrayUtils;
```

### Parameter

* `$callback` <img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;

  > A function to compare element for sort, taking two arguments:
  >
  > * `$a` The comparison target A
  > * `$b` The comparison target B
  >
  > Default is `NULL`, If is null, Sort by default sort function.

### Return value

* A sorted array.

## Prefixing

```php
$arrayUtils->sortKeyAs(?callable $callback = null) : array;
```

```php
ArrayUtils::sortKeyFrom(iterable $from, ?callable $callback = null) : ArrayUtils;
```

```php
ArrayUtils::sortKeyFromAs(iterable $from, ?callable $callback = null) : array;
```

## References

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

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


# unique()

The unique() method removes duplicate values

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

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

$arrayUtils = ArrayUtils::from(["a", "a", "a", "b", "c", "c", "d"]);

$arrayUtils->unique();
// expected output: ["a", "b", "c", "d"]
```

{% endcode %}

## Syntax

```php
$arrayUtils->unique(int $sort_flags = SORT_STRING) : ArrayUtils;
```

### Parameter

* `$sortFlags` <img src="https://2976351099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKYhyHaArG9Gsnmdxc8%2F-MKk9I82AGQBHkmwnVvk%2F-MKk9YjCz_YkVR1YiRy-%2FBADGE_OPTIONAL.svg?alt=media&amp;token=3fbbac84-2f1b-40af-a991-b2eff659866a" alt="" data-size="line">&#x20;

  > Used to modify the sorting behavior using these values:
  >
  > Sorting type flags:
  >
  > * **`SORT_REGULAR`** - compare items normally (don't change types)
  > * **`SORT_NUMERIC`** - compare items numerically
  > * **`SORT_STRING`** - compare items as strings
  > * **`SORT_LOCALE_STRING`** - compare items as strings, based on the current locale.

### Return value

* A filtered array.

## Prefixing

```php
$arrayUtils->uniqueAs(int $sort_flags = SORT_STRING) : array;
```

```php
ArrayUtils::uniqueFrom(iterable $from, int $sort_flags = SORT_STRING) : ArrayUtils;
```

```php
ArrayUtils::uniqueFromAs(iterable $from, int $sort_flags = SORT_STRING) : array;
```

## References

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


# unshift()

The unshift() method all similar to push(), but this push elements onto the start of array

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

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

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

//Unshift 0 and 10
$arrayUtils->unshift(0, 10);
// expected output: [0, 10, 1, 2, 3, 4, 5]
```

{% endcode %}

## Syntax

```php
$arrayUtils->unshift(mixed ...$values) : ArrayUtils;
```

### Parameter

* `$values`

  > A values to unshift into array

###

### Return value

* oneself back for method chaining.

## Prefixing

```php
$arrayUtils->unshiftAs(mixed ...$values) : array;
```

```php
ArrayUtils::unshiftFrom(iterable $from, mixed ...$values) : ArrayUtils;
```

```php
ArrayUtils::unshiftFromAs(iterable $from, mixed ...$values) : array;
```

## References

{% content-ref url="/pages/-MKmSpTJX9Uo5R5MdlG4" %}
[push()](/methods/c/push)
{% endcontent-ref %}

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

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift>" %}


# values()

The values() method return all the values of an array

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

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

$arrayUtils = ArrayUtils::from(["first" => 1, "second" => 2, "third" => 3]);

//Get all values
$arrayUtils->values();
// expected output: [1, 2, 3]
```

{% endcode %}

## Syntax

```php
$arrayUtils->values() : ArrayUtils;
```

### Return value

* &#x20;A array of all the values in `array`.

## Prefixing

```php
$arrayUtils->valuesAs() : array;
```

```php
ArrayUtils::valuesFrom(iterable $from) : ArrayUtils;
```

```php
ArrayUtils::valuesFromAs(iterable $from) : array;
```

## References

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


# 📖Suffixes

{% hint style="info" %}

## Modifiers are divided into two

{% endhint %}

{% content-ref url="/pages/-MKpIDQbxjHxelU6Oe9-" %}
[Suffix - From](/prefixs/from)
{% endcontent-ref %}

{% content-ref url="/pages/-MKpMOnqm75Parhz3L6k" %}
[Suffix - As](/prefixs/as)
{% endcontent-ref %}


# Suffix - From

Suffixing "From" to the any methods can called statically.

### Suffixing "From" to the any methods can called statically. It can be used by giving an `iterable` as the first argument.

{% tabs %}
{% tab title="Example" %}

```php
$arr = ArrayUtils::reverseFrom([1,2,3,4,5]);

var_export((array) $arr);
//array (0 => 5, 1 => 4, 2 => 3, 3 => 2, 4 => 1
```

{% endtab %}

{% tab title="is same to" %}

```php
$arr = ArrayUtils::from([1,2,3,4,5])->reverse();

var_export((array) $arr);
//array (0 => 5, 1 => 4, 2 => 3, 3 => 2, 4 => 1
```

{% endtab %}
{% endtabs %}


# Suffix - As

Suffixing "As" to the chaining method for returns a pure array.

### Suffixing "As" to the chaining method for returns a pure array. Method chaining breaks if you use this Suffix.

{% tabs %}
{% tab title="Example" %}

```php
$arr = ArrayUtils::from([1,2,3,4,5]);

var_export($arr->reverseAs());
//array (0 => 5, 1 => 4, 2 => 3, 3 => 2, 4 => 1)
```

{% endtab %}

{% tab title="is same to" %}

```php
$arr = ArrayUtils::from([1,2,3,4,5]);

var_export((array) $arr->reverse());
//array (0 => 5, 1 => 4, 2 => 3, 3 => 2, 4 => 1)
```

{% endtab %}
{% endtabs %}


