PHPackages                             bocharsky-bw/arrayzy - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. bocharsky-bw/arrayzy

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

bocharsky-bw/arrayzy
====================

A native PHP arrays easy manipulation library in OOP way.

v0.6.1(9y ago)38425.4k—6.1%37[1 issues](https://github.com/bocharsky-bw/Arrayzy/issues)[1 PRs](https://github.com/bocharsky-bw/Arrayzy/pulls)MITPHPPHP &gt;=5.4

Since Feb 28Pushed 8y ago18 watchersCompare

[ Source](https://github.com/bocharsky-bw/Arrayzy)[ Packagist](https://packagist.org/packages/bocharsky-bw/arrayzy)[ Docs](https://github.com/bocharsky-bw/Arrayzy)[ RSS](/packages/bocharsky-bw-arrayzy/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (13)Used By (0)

Arrayzy
=======

[](#arrayzy)

The wrapper for all PHP built-in array functions and easy, object-oriented array manipulation library. In short: Arrays on steroids.

[![Travis Status](https://camo.githubusercontent.com/70dbd75ba089a95d1e62249ab6a22aff459d8f66b43f5b679332a77f52709b2f/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f626f63686172736b792d62772f41727261797a792f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/bocharsky-bw/Arrayzy)[![HHVM Status](https://camo.githubusercontent.com/0041251b6a3d74508d87b21de8f8ae155acfb752e2d6055c08b6c167d5ad343e/68747470733a2f2f696d672e736869656c64732e696f2f6868766d2f626f63686172736b792d62772f61727261797a792f6d61737465722e7376673f7374796c653d666c61742d737175617265)](http://hhvm.h4cc.de/package/bocharsky-bw/arrayzy)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/80d98891a07826744be75f622de21095301508a21c91d0284abc9280d1157441/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f626f63686172736b792d62772f41727261797a792e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/bocharsky-bw/Arrayzy/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/affbf4c02082bb3c469d1b2c3fde19882649d7d1ffe9a624309c6f15bd79f67d/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f626f63686172736b792d62772f41727261797a792e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/bocharsky-bw/Arrayzy/?branch=master)[![Software License](https://camo.githubusercontent.com/942e017bf0672002dd32a857c95d66f28c5900ab541838c6c664442516309c8a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265)](https://github.com/bocharsky-bw/Arrayzy/blob/master/LICENSE)[![Latest Version](https://camo.githubusercontent.com/265e9ae0220dbbee5b81b4acff8c3f67ce3f38a8191b172758c51feb8df161c8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f626f63686172736b792d62772f61727261797a792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bocharsky-bw/arrayzy)

[![SensioLabsInsight](https://camo.githubusercontent.com/05431b17125f0fb2293d7ce7985d1d6ad9f0bd69b6bf08079beb87cc49be7a99/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f65303233356635642d613839622d346164642d623363362d3435383133643262663965622f6269672e706e67)](https://insight.sensiolabs.com/projects/e0235f5d-a89b-4add-b3c6-45813d2bf9eb)

ArrayImitator
-------------

[](#arrayimitator)

This is the *main* class of this library. Each method, which associated with the corresponding native PHP function, keep its behavior. In other words: methods could creates a new array (leaving the original array unchanged), operates on the same array (returns the array itself and **DOES NOT** create a new instance) or return some result.

> **NOTE:** If method creates a new array but you don't need the first array you operate on, you can override it manually:

```
use Arrayzy\ArrayImitator as A;

$a = A::create(['a', 'b', 'c']);
$a = $a->reverse(); // override instance you operates on, because $a !== $a->reverse()
```

> **NOTE:** If method operates on the same array but you need to keep the first array you operate on as unchanged, you can clone it manually first:

```
use Arrayzy\ArrayImitator as A;

$a = A::create(['a', 'b', 'c']);
$b = clone $a;
$b->shuffle(); // keeps $a unchanged, because $a !== $b
```

Contents
========

[](#contents)

- [Requirements](#requirements)
- [Installation](#installation)
- [Creation](#creation)
- [Usage](#usage)
    - [Chaining](#chaining)
- [Public method list](#public-method-list)
    - [add](#add)
    - [chunk](#chunk)
    - [clear](#clear)
    - [combine](#combine)
    - [contains](#contains)
    - [containsKey](#containskey)
    - [count](#count)
    - [create](#create)
    - [createClone](#createclone)
    - [createFromJson](#createfromjson)
    - [createFromObject](#createfromobject)
    - [createFromString](#createfromstring)
    - [createWithRange](#createwithrange)
    - [current](#current)
    - [customSort](#customsort)
    - [customSortKeys](#customsortkeys)
    - [debug](#debug)
    - [diff](#diff)
    - [each](#each)
    - [end](#end)
    - [except](#except)
    - [exists](#exists)
    - [export](#export)
    - [filter](#filter)
    - [find](#find)
    - [first](#first)
    - [flip](#flip)
    - [getIterator](#getiterator)
    - [getKeys](#getkeys)
    - [getRandom](#getrandom)
    - [getRandomKey](#getrandomkey)
    - [getRandomKeys](#getrandomkeys)
    - [getRandomValues](#getrandomvalues)
    - [getValues](#getvalues)
    - [indexOf](#indexof)
    - [intersect](#intersect)
    - [intersectAssoc](#intersectAssoc)
    - [intersectKey](#intersectKey)
    - [isAssoc](#isassoc)
    - [isEmpty](#isempty)
    - [isNumeric](#isnumeric)
    - [key](#key)
    - [last](#last)
    - [map](#map)
    - [merge](#merge)
    - [next](#next)
    - [offsetExists](#offsetexists)
    - [offsetGet](#offsetget)
    - [offsetSet](#offsetset)
    - [offsetUnset](#offsetunset)
    - [only](#only)
    - [pad](#pad)
    - [pop](#pop)
    - [previous](#previous)
    - [push](#push)
    - [reduce](#reduce)
    - [reindex](#reindex)
    - [replace](#replace)
    - [reset](#reset)
    - [reverse](#reverse)
    - [search](#search)
    - [shift](#shift)
    - [shuffle](#shuffle)
    - [slice](#slice)
    - [sort](#sort)
    - [sortKeys](#sortkeys)
    - [toArray](#toarray)
    - [toJson](#tojson)
    - [toReadableString](#toreadablestring)
    - [toString](#tostring)
    - [unique](#unique)
    - [unshift](#unshift)
    - [walk](#walk)
- [Contribution](#contribution)
- [Links](#links)

Requirements
------------

[](#requirements)

- PHP `5.4` or higher
- PHP `JSON` extension

Installation
------------

[](#installation)

The preferred way to install this package is to use [Composer](https://getcomposer.org/):

```
$ composer require bocharsky-bw/arrayzy
```

If you don't use `Composer` - register this package in your autoloader manually or download this library and `require` the necessary files directly in your scripts:

```
require_once __DIR__ . '/path/to/library/src/ArrayImitator.php';
```

Creation
--------

[](#creation)

Create a new empty array with the `new` statement.

```
use Arrayzy\ArrayImitator;

$a = new ArrayImitator; // Creates a new instance with the "use" statement
// or
$a = new \Arrayzy\ArrayImitator; // Creates a new array by fully qualified namespace
```

> **NOTE:** Don't forget about namespaces. You can use [namespace aliases](https://secure.php.net/manual/en/language.namespaces.importing.php)for simplicity if you want:

```
use Arrayzy\ArrayImitator as A;

$a = new A; // Creates a new instance using namespace alias
```

Create a new array with default values, passed it to the constructor as an array:

```
$a = new A([1, 2, 3]);
// or
$a = new A([1 => 'a', 2 => 'b', 3 => 'c']);
```

Also, new objects can be created with one of the public static methods prefixed with 'create':

- [create](#create)
- [createFromJson](#createfromjson)
- [createFromObject](#createfromobject)
- [createFromString](#createfromstring)
- [createWithRange](#createwithrange)

Usage
-----

[](#usage)

You can get access to the values like with the familiar PHP array syntax:

```
use Arrayzy\ArrayImitator as A;

$a = A::create(['a', 'b', 'c']);

$a[] = 'e';    // or use $a->offsetSet(null, 'e') method
$a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c', 3 => 'e']

$a[3] = 'd';   // or use $a->offsetSet(3, 'd') method
$a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd']

print $a[1]; // 'b'
// or use the corresponding method
print $a->offsetGet(1); // 'b'
```

**NOTE:** *The following methods and principles apply to the `ArrayImitator` class. In the examples provided below the `ArrayImitator` aliased with `A`.*

### Chaining

[](#chaining)

Methods may be chained for ease of use:

```
$a = A::create(['a', 'b', 'c']);

$a
    ->offsetSet(null, 'e')
    ->offsetSet(3, 'd')
    ->offsetSet(null, 'e')
    ->shuffle() // or any other method that returns $this
;

$a->toArray(); // [0 => 'c', 1 => 'a', 2 => 'e', 3 => 'd', 4 => 'b']
```

### Converting

[](#converting)

Easily convert instance array elements to a simple PHP `array`, `string`, readable `string` or JSON format:

- [toArray](#toarray)
- [toJson](#tojson)
- [toReadableString](#toreadablestring)
- [toString](#tostring)

### Debugging

[](#debugging)

- [debug](#debug)
- [export](#export)

Public method list
------------------

[](#public-method-list)

### add

[](#add)

> Associated with `$a[] = 'new item'`.

```
$a = A::create(['a', 'b', 'c']);
$a->add('d');
$a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd']
```

### chunk

[](#chunk)

> Associated with [array\_chunk()](https://secure.php.net/manual/en/function.array-chunk.php).

```
$a = A::create(['a', 'b', 'c']);
$a = $a->chunk(2);
$a->toArray(); // [0 => [0 => 'a', 1 => 'b'], 1 => [0 => 'c']]
```

### clear

[](#clear)

> Associated with `$a = []`.

```
$a = A::create(['a', 'b', 'c']);
$a->clear();
$a->toArray(); // []
```

### combine

[](#combine)

> Associated with [array\_combine()](https://secure.php.net/manual/en/function.array-combine.php).

```
$a = A::create([1, 2, 3]);
$a->combine(['a', 'b', 'c']);
$a->toArray(); // [1 => 'a', 2 => 'b', 3 => 'c']
```

### contains

[](#contains)

> Associated with [in\_array()](https://secure.php.net/manual/en/function.in-array.php).

```
$a = A::create(['a', 'b', 'c']);
$a->contains('c'); // true
```

### containsKey

[](#containskey)

> Associated with [array\_key\_exists()](https://secure.php.net/manual/en/function.array-key-exists.php).

```
$a = A::create(['a', 'b', 'c']);
$a->containsKey(2); // true
```

### count

[](#count)

> Associated with [count()](https://secure.php.net/manual/en/function.count.php).

```
$a = A::create(['a', 'b', 'c']);
$a->count(); // 3
```

### create

[](#create)

```
$a = A::create(['a', 'b', 'c']);
$a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c']
```

### createClone

[](#createclone)

Creates a shallow copy of the array.

Keep in mind, that in PHP variables contain only references to the object, **NOT** the object itself:

```
$a = A::create(['a', 'b', 'c']);
$b = $a; // $a and $b are different variables referencing the same object ($a === $b)
```

So if you **DO NOT** want to modify the current array, you need to clone it manually first:

```
$a = A::create(['a', 'b', 'c']);
$b = clone $a; // $a and $b are different instances ($a !== $b)
// or do it with built-in method
$b = $a->createClone(); // $a !== $b
```

### createFromJson

[](#createfromjson)

> Associated with [json\_decode()](https://secure.php.net/manual/en/function.json-decode.php).

Creates an array by parsing a JSON string:

```
$a = A::createFromJson('{"a": 1, "b": 2, "c": 3}');
$a->toArray(); // ['a' => 1, 'b' => 2, 'c' => 3]
```

### createFromObject

[](#createfromobject)

Creates an instance array from any `object` that implemented `\ArrayAccess` interface:

```
$a = A::create(['a', 'b', 'c']);
$b = A::createFromObject($a); // where $a could be any object that implemented \ArrayAccess interface
$b->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c']
```

### createFromString

[](#createfromstring)

> Associated with [explode()](https://secure.php.net/manual/en/function.explode.php).

Creates an array from a simple PHP `string` with specified separator:

```
$a = A::createFromString('a;b;c', ';');
$a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c']
```

### createWithRange

[](#createwithrange)

> Associated with [range()](https://secure.php.net/manual/en/function.range.php).

Creates an array of a specified range:

```
$a = A::createWithRange(2, 6, 2);
$a->toArray(); // [0 => 2, 1 => 4, 2 => 6]
```

### current

[](#current)

> Associated with [current()](https://secure.php.net/manual/en/function.current.php).

Position of the iterator.

```
$a = A::create(['a', 'b', 'c']);
$a->current(); // 'a'
```

### customSort

[](#customsort)

> Associated with [usort()](https://secure.php.net/manual/en/function.usort.php).

```
$a = A::create(['b', 'a', 'c']);
$a->customSort(function($a, $b) {
    if ($a === $b) {
        return 0;
    }

    return ($a < $b) ? -1 : 1;
});
$a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c']
```

### customSortKeys

[](#customsortkeys)

> Associated with [uksort()](https://secure.php.net/manual/en/function.uksort.php).

```
$a = A::create([1 => 'b', 0 => 'a', 2 => 'c']);
$a->customSortKeys(function($a, $b) {
    if ($a === $b) {
        return 0;
    }

    return ($a < $b) ? -1 : 1;
});
$a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c']
```

### debug

[](#debug)

> Associated with [print\_r()](https://secure.php.net/manual/en/function.print-r.php).

```
$a = A::create(['a', 'b', 'c']);
$a->debug(); // Array ( [0] => a [1] => b [2] => c )
```

### diff

[](#diff)

> Associated with [array\_diff()](https://secure.php.net/manual/en/function.array-diff.php).

```
$a = A::create(['a', 'b', 'c']);
$a = $a->diff(['c', 'd']);
$a->toArray(); // [0 => 'a', 1 => 'b']
```

### each

[](#each)

> Associated with [each()](https://secure.php.net/manual/en/function.each.php).

```
$a = A::create(['a', 'b', 'c']);
$a->each(); // [0 => 0, 'key' => 0, 1 => 'a', 'value' => 'a']
```

### end

[](#end)

> Associated with [end()](https://secure.php.net/manual/en/function.end.php).

```
$a = A::create(['a', 'b', 'c']);
$a->end(); // 'c'
```

### except

[](#except)

> Based on [array\_diff\_key()](https://secure.php.net/manual/en/function.array-diff-key.php).

Chunk of an array without given keys.

```
$a = A::create(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5]);
$a = $a->except(['b', 'e']);
$a->toArray(); // ['a' => 1, 'c' => 3, 'd' => 4]
```

### exists

[](#exists)

A custom contains method where you can supply your own custom logic in any callable function.

```
$a = A::create(['a', 'b', 'c']);

$a->exists(function($key, $value) {
   return 1 === $key and 'b' === $value;
}); // true
```

### export

[](#export)

> Associated with [var\_export()](https://secure.php.net/manual/en/function.var-export.php).

```
$a = A::create(['a', 'b', 'c']);
$a->export(); // array ( 0 => 'a', 1 => 'b', 2 => 'c', )
```

### filter

[](#filter)

> Associated with [array\_filter()](https://secure.php.net/manual/en/function.array-filter.php).

```
$a = A::create(['a', 'z', 'b', 'z']);
$a = $a->filter(function($value) {
    return 'z' !== $value; // exclude 'z' value from array
});
$a->toArray(); // [0 => 'a', 2 => 'b']
```

### find

[](#find)

A custom find method where you can supply your own custom logic in any callable function.

```
$a = A::create(['a', 'b', 'c']);
$a->find(function($value, $key) {
    return 'b' == $value && 0 < $key;
}); // 'b'
```

### first

[](#first)

> Alias of [reset](#reset).

```
$a = A::create(['a', 'b', 'c']);
$a->first(); // 'a'
```

### flip

[](#flip)

> Associated with [array\_flip()](https://secure.php.net/manual/en/function.array-flip.php).

```
$a = A::create(['a', 'b', 'c']);
$a = $a->flip();
$a->toArray(); // ['a' => 0, 'b' => 1, 'c' => 2]
```

### getIterator

[](#getiterator)

Creates an external Iterator. Check the [iteratorAggregate](https://secure.php.net/manual/en/class.iteratoraggregate.php) documentation for more information.

### getKeys

[](#getkeys)

> Associated with [array\_keys()](https://secure.php.net/manual/en/function.array-keys.php).

```
$a = A::create(['a' => 1, 'b' => 2, 'c' => 3]);
$a->getKeys(); // [0 => 'a', 1 => 'b', 2 => 'c']
```

### getRandom

[](#getrandom)

> Associated with [array\_rand()](https://secure.php.net/manual/en/function.array-rand.php).

```
$a = A::create(['a', 'b', 'c', 'd']);
$a->getRandom(); // 'c'
```

### getRandomKey

[](#getrandomkey)

> Associated with [array\_rand()](https://secure.php.net/manual/en/function.array-rand.php).

```
$a = A::create(['a', 'b', 'c', 'd']);
$a->getRandomKey(); // 2
```

### getRandomKeys

[](#getrandomkeys)

> Associated with [array\_rand()](https://secure.php.net/manual/en/function.array-rand.php).

```
$a = A::create(['a', 'b', 'c', 'd']);
$a->getRandomKeys(2); // [0, 2]
```

### getRandomValues

[](#getrandomvalues)

> Associated with [array\_rand()](https://secure.php.net/manual/en/function.array-rand.php).

```
$a = A::create(['a', 'b', 'c', 'd']);
$a->getRandomValues(2); // ['b', 'd']
```

### getValues

[](#getvalues)

> Associated with [array\_values()](https://secure.php.net/manual/en/function.array-values.php).

```
$a = A::create([1 => 'a', 2 => 'b', 3 => 'c']);
$a->getValues(); // [0 => 'a', 1 => 'b', 2 => 'c']
```

### indexOf

[](#indexof)

> Alias of [search](#search).

```
$a = A::create(['a', 'b', 'c']);
$a->indexOf('b'); // 1
```

### intersect

[](#intersect)

> Associated with [array\_intersect()](https://secure.php.net/manual/en/function.array-intersect.php).

```
$a = A::create(['a', 'b', 'c']);
$a = $a->intersect(['b', 'c']);
$a->toArray(); // [1 => 'b', 2 => 'c']
```

### intersectAssoc

[](#intersectassoc)

> Associated with [array\_intersect\_assoc()](https://secure.php.net/manual/en/function.array-intersect-assoc.php).

```
$a = A::create(['one' => 'a', 'two' => 'b', 'three' => 'c']);
$a = $a->intersectAssoc(['two' => 'b', 'four' => 'c']);
$a->toArray(); // ['two' => 'b']
```

### intersectKey

[](#intersectkey)

> Associated with [array\_intersect\_key()](https://secure.php.net/manual/en/function.array-intersect-key.php).

```
$a = A::create(['one' => 'a', 'two' => 'b', 'three' => 'c']);
$a = $a->intersectKey(['two' => 'd', 'three' => 'e']);
$a->toArray(); // ['two' => 'b', 'three' => 'c']
```

### isAssoc

[](#isassoc)

Check whether all array keys are *associative*.

```
$a = A::create(['key' => 'value']);
$a->isAssoc(); // true
```

### isEmpty

[](#isempty)

Check whether array is empty.

```
$a = A::create([]);
$a->isEmpty(); // true
```

### isNumeric

[](#isnumeric)

Check whether all array keys are *numeric*.

```
$a = A::create(['a', 'b', 'c']);
$a->isNumeric(); // true
```

### key

[](#key)

> Associated with [key()](https://secure.php.net/manual/en/function.key.php).

```
$a = A::create(['a', 'b', 'c']);
$a->current(); // 'a'
$a->key();     // 0
$a->next();    // 'b'
$a->key();     // 1
```

### last

[](#last)

> Alias of [end](#end).

```
$a = A::create(['a', 'b', 'c']);
$a->last(); // 'c'
```

### map

[](#map)

> Associated with [array\_map()](https://secure.php.net/manual/en/function.array-map.php).

```
$a = A::create(['a', 'b', 'c']);
$a = $a->map(function($value) {
    return $value . $value;
});
$a->toArray(); // [0 => 'aa', 1 => 'bb', 2 => 'cc']
```

### merge

[](#merge)

> Associated with [array\_merge()](https://secure.php.net/manual/en/function.array-merge.php) / [array\_merge\_recursive()](https://secure.php.net/manual/en/function.array-merge-recursive.php).

```
// indexed array behavior
$a = A::create(['a', 'b', 'c']); // create indexed array
$a = $a->merge(['c', 'd']);
$a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c', 3 => 'c', 4 => 'd']

// assoc array behavior
$b = A::create(['a' => 1, 'b' => 2, 'c' => 99]); // create assoc array
$b = $b->merge(['c' => 3, 'd' => 4]);
$b->toArray(); // ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]
```

### next

[](#next)

> Associated with [next()](https://secure.php.net/manual/en/function.next.php).

```
$a = A::create(['a', 'b', 'c']);
$a->next(); // 'b'
$a->next(); // 'c'
```

### offsetExists

[](#offsetexists)

> Implemented for [ArrayAccess](https://secure.php.net/manual/en/arrayaccess.offsetexists.php) interface.

```
$a = A::create(['a', 'b', 'c']);
$a->offsetExists(2); // true (or use isset($a[2]))
$a->offsetExists(3); // false (or use isset($a[3]))
```

### offsetGet

[](#offsetget)

> Implemented for [ArrayAccess](https://secure.php.net/manual/en/arrayaccess.offsetget.php) interface.

```
$a = A::create(['a', 'b', 'c']);
$a->offsetGet(1); // 'b' (or use $a[1])
```

### offsetSet

[](#offsetset)

> Implemented for [ArrayAccess](https://secure.php.net/manual/en/arrayaccess.offsetset.php) interface.

```
$a = A::create(['a', 'b', 'd']);
// add a new value
$a->offsetSet(null, 'd'); // or use $a[] = 'd';
$a->toArray();            // [0 => 'a', 1 => 'b', 2 => 'd', 3=> 'd']
// replace an existing value by key
$a->offsetSet(2, 'c');    // or use $a[2] = 'c';
$a->toArray();            // [0 => 'a', 1 => 'b', 2 => 'c', 3=> 'd']
```

### offsetUnset

[](#offsetunset)

> Implemented for [ArrayAccess](https://secure.php.net/manual/en/arrayaccess.offsetunset.php) interface.

```
$a = A::create(['a', 'b', 'c']);
$a->offsetUnset(1); // or use unset($a[1]);
$a->toArray();      // [0 => 'a', 2 => 'c']
```

### only

[](#only)

> Based on [array\_intersect\_key()](https://secure.php.net/manual/en/function.array-intersect-key.php).

Chunk of an array with only given keys.

```
$a = A::create(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5]);
$a = $a->only(['b', 'e']);
$a->toArray(); // ['b' => 2, 'e' => 5]
```

### pad

[](#pad)

> Associated with [array\_pad()](https://secure.php.net/manual/en/function.array-pad.php).

```
$a = A::create(['a', 'b', 'c']);
$a = $a->pad(5, 'z');
$a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c', 3 => 'z', 4 => 'z']
```

### pop

[](#pop)

> Associated with [array\_pop()](https://secure.php.net/manual/en/function.array-pop.php).

```
$a = A::create(['a', 'b', 'c']);
$a->pop();     // 'c'
$a->toArray(); // [0 => 'a', 1 => 'b']
```

### previous

[](#previous)

> Associated with [prev()](https://secure.php.net/manual/en/function.prev.php).

```
$a = A::create(['a', 'b', 'c']);
$a->next();     // 'b'
$a->next();     // 'c'
$a->previous(); // 'b'
```

### push

[](#push)

> Associated with [array\_push()](https://secure.php.net/manual/en/function.array-push.php).

```
$a = A::create(['a', 'b']);
$a->push('c', 'd');
$a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd']
```

> The `push()` method allows multiple arguments.

### reduce

[](#reduce)

> Associated with [array\_reduce()](https://secure.php.net/manual/en/function.array-reduce.php).

```
$a = A::create(['a', 'b', 'c']);
$a = $a->reduce(function($result, $item) {
    return $result . $item;
}); // 'abc'
```

### reindex

[](#reindex)

> Based on [array\_values()](https://secure.php.net/manual/en/function.array-values.php).

```
$a = A::create([2 => 'a', 1 => 'b', 3 => 'c']);
$a = $a->reindex();
$a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c']
```

### replace

[](#replace)

> Associated with [array\_replace()](https://secure.php.net/manual/en/function.array-replace.php) / [array\_replace\_recursive()](https://secure.php.net/manual/en/function.array-replace-recursive.php).

```
$a = A::create(['a', 'd', 'e']);
$a = $a->replace([1 => 'b', 2 => 'c']);
$a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c']
```

### reset

[](#reset)

> Associated with [reset()](https://secure.php.net/manual/en/function.reset.php).

```
$a = A::create(['a', 'b', 'c']);
$a->reset(); // 'a'
```

### reverse

[](#reverse)

> Associated with [array\_reverse()](https://secure.php.net/manual/en/function.array-reverse.php).

```
$a = A::create(['a', 'b', 'c']);
$a = $a->reverse();
$a->toArray(); // [0 => 'c', 1 => 'b', 2 => 'a']
```

### search

[](#search)

> Associated with [array\_search()](https://secure.php.net/manual/en/function.array-search.php).

```
$a = A::create(['a', 'b', 'c']);
$a->search('b'); // 1
```

### shift

[](#shift)

> Associated with [array\_shift()](https://secure.php.net/manual/en/function.array-shift.php).

```
$a = A::create(['a', 'b', 'c']);
$a->shift();
$a->toArray(); // [0 => 'b', 1 => 'c']
```

### shuffle

[](#shuffle)

> Associated with [shuffle()](https://secure.php.net/manual/en/function.shuffle.php).

```
$a = A::create(['a', 'b', 'c']);
$a->shuffle();
$a->toArray(); // [0 => 'c', 1 => 'a', 2 => 'b']
```

### slice

[](#slice)

> Associated with [array\_slice()](https://secure.php.net/manual/en/function.array-slice.php).

```
$a = A::create(['a', 'b', 'c', 'd']);
$a = $a->slice(1, 2);
$a->toArray(); // [0 => 'b', 1 => 'c']
```

### sort

[](#sort)

> Associated with [arsort()](https://secure.php.net/manual/en/function.arsort.php) / [sort()](https://secure.php.net/manual/en/function.sort.php) / [asort()](https://secure.php.net/manual/en/function.asort.php) / [rsort()](https://secure.php.net/manual/en/function.rsort.php).

```
$a = A::create(['b', 'a', 'd', 'c']);
$a->sort(SORT_DESC);
$a->toArray(); // [0 => 'd', 1 => 'c', 2 => 'b', 3 => 'a']
```

### sortKeys

[](#sortkeys)

> Associated with [ksort()](https://secure.php.net/manual/en/function.ksort.php) / [krsort()](https://secure.php.net/manual/en/function.krsort.php).

```
$a = A::create([3 => 'a', 1 => 'b', 2 => 'c', 0 => 'd']);
$a->sortKeys(SORT_ASC);
$a->toArray(); // [0 => 'd', 1 => 'b', 2 => 'c', 3 => 'a']
```

### toArray

[](#toarray)

Convert the array to a simple PHP `array` type:

```
$a = A::create(['a', 'b', 'c']);
$a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c']
```

### toJson

[](#tojson)

> Associated with [json\_encode()](https://secure.php.net/manual/en/function.json-encode.php).

Creates a JSON string from the array:

```
$a = A::create(['a' => 1, 'b' => 2, 'c' => 3]);
$a->toJson(); // { "a": 1, "b": 2, "c": 3 }
```

### toReadableString

[](#toreadablestring)

> Based on [implode()](https://secure.php.net/manual/en/function.implode.php).

Converts instance array to a readable PHP `string`:

```
$a = A::create(['a', 'b', 'c']);
$a->toReadableString(', ', ' and '); // 'a, b and c'
```

### toString

[](#tostring)

> Associated with [implode()](https://secure.php.net/manual/en/function.implode.php).

Converts instance array to a simple PHP `string`:

```
$a = A::create(['a', 'b', 'c']);
$a->toString(', '); // 'a, b, c'
```

### unique

[](#unique)

> Associated with [array\_unique()](https://secure.php.net/manual/en/function.array-unique.php).

```
$a = A::create(['a', 'b', 'b', 'c']);
$a = $a->unique();
$a->toArray(); // [0 => 'a', 1 => 'b', 3 => 'c']
```

### unshift

[](#unshift)

> Associated with [array\_unshift()](https://secure.php.net/manual/en/function.array-unshift.php).

```
$a = A::create(['a', 'b']);
$a->unshift('y', 'z');
$a->toArray(); // [0 => 'y', 1 => 'z', 2 => 'a', 3 => 'b']
```

> Method `unshift()` allow multiple arguments.

### walk

[](#walk)

> Associated with [array\_walk()](https://secure.php.net/manual/en/function.array-walk.php) / [array\_walk\_recursive()](https://secure.php.net/manual/en/function.array-walk-recursive.php).

```
$a = A::create(['a', 'b', 'c']);
$a->walk(function(&$value, $key) {
    $key++; // the $key variable passed by value, (original value will not modified)
    $value = $value . $key; // the $value variable passed by reference (modifies original value)
});
$a->toArray(); // [0 => 'a1', 1 => 'b2', 2 => 'c3']
```

Contribution
------------

[](#contribution)

Feel free to submit an [Issue](https://github.com/bocharsky-bw/Arrayzy/issues) or create a [Pull Request](https://github.com/bocharsky-bw/Arrayzy/pulls) if you find a bug or just want to propose an improvement suggestion.

In order to propose a new feature the best way is to submit an [Issue](https://github.com/bocharsky-bw/Arrayzy/issues) and discuss it first.

Links
-----

[](#links)

**Arrayzy** was inspired by Doctrine [ArrayCollection](https://github.com/doctrine/collections/blob/master/lib/Doctrine/Common/Collections/ArrayCollection.php) class and [Stringy](https://github.com/danielstjules/Stringy) library.

Look at the [Stringy](https://github.com/danielstjules/Stringy) if you are looking for a PHP *string* manipulation library in an OOP way.

[Move UP](#arrayzy)

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity49

Moderate usage in the ecosystem

Community25

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~63 days

Total

9

Last Release

3589d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ce9dde2c8d3d5fcfdbb5427f981df17123b54b410e21e4dfc933f07983eefeba?d=identicon)[bocharsky-bw](/maintainers/bocharsky-bw)

---

Top Contributors

[![bocharsky-bw](https://avatars.githubusercontent.com/u/3317635?v=4)](https://github.com/bocharsky-bw "bocharsky-bw (159 commits)")[![JustusAdam](https://avatars.githubusercontent.com/u/5824532?v=4)](https://github.com/JustusAdam "JustusAdam (5 commits)")[![talyssonoc](https://avatars.githubusercontent.com/u/4325587?v=4)](https://github.com/talyssonoc "talyssonoc (4 commits)")[![m1](https://avatars.githubusercontent.com/u/978089?v=4)](https://github.com/m1 "m1 (2 commits)")[![bluemanos](https://avatars.githubusercontent.com/u/9991002?v=4)](https://github.com/bluemanos "bluemanos (1 commits)")[![znck](https://avatars.githubusercontent.com/u/2596484?v=4)](https://github.com/znck "znck (1 commits)")[![pmysiak](https://avatars.githubusercontent.com/u/1159674?v=4)](https://github.com/pmysiak "pmysiak (1 commits)")[![visualfanatic](https://avatars.githubusercontent.com/u/106248819?v=4)](https://github.com/visualfanatic "visualfanatic (1 commits)")[![XedinUnknown](https://avatars.githubusercontent.com/u/1428973?v=4)](https://github.com/XedinUnknown "XedinUnknown (1 commits)")

---

Tags

helperarrayutilitymanipulationwrapperOOParrayzy

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/bocharsky-bw-arrayzy/health.svg)

```
[![Health](https://phpackages.com/badges/bocharsky-bw-arrayzy/health.svg)](https://phpackages.com/packages/bocharsky-bw-arrayzy)
```

###  Alternatives

[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k394.3M1.5k](/packages/nette-utils)[danielstjules/stringy

A string manipulation library with multibyte support

2.4k26.0M191](/packages/danielstjules-stringy)[voku/arrayy

Array manipulation library for PHP, called Arrayy!

4875.5M16](/packages/voku-arrayy)[voku/stringy

A string manipulation library with multibyte support

1783.8M19](/packages/voku-stringy)[samrap/acf-fluent

A fluent interface for the Advanced Custom Fields WordPress plugin

28656.0k4](/packages/samrap-acf-fluent)[spatie/array-functions

Some handy array helpers

24561.1k2](/packages/spatie-array-functions)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
