PHPackages                             dtkahl/php-array-tools - 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. dtkahl/php-array-tools

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

dtkahl/php-array-tools
======================

3.1.1(8y ago)92.5k[7 issues](https://github.com/dtkahl/php-array-tools/issues)4MITPHPPHP &gt;=5.6.0

Since Mar 15Pushed 8y ago1 watchersCompare

[ Source](https://github.com/dtkahl/php-array-tools)[ Packagist](https://packagist.org/packages/dtkahl/php-array-tools)[ RSS](/packages/dtkahl-php-array-tools/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (10)Dependencies (1)Versions (29)Used By (4)

[![Latest Stable Version](https://camo.githubusercontent.com/5b24e8bfd0eed97bd94aa8cc63d870513f8ccc0fe42fc38e5dfac9d6c8a86580/68747470733a2f2f706f7365722e707567782e6f72672f64746b61686c2f7068702d61727261792d746f6f6c732f762f737461626c65)](https://packagist.org/packages/dtkahl/php-array-tools)[![License](https://camo.githubusercontent.com/c2132ad72cb045c719f41da42317cf75dd91fb7ccd250bdc1824b25fbd964c51/68747470733a2f2f706f7365722e707567782e6f72672f64746b61686c2f7068702d61727261792d746f6f6c732f6c6963656e7365)](https://packagist.org/packages/dtkahl/php-array-tools)[![Build Status](https://camo.githubusercontent.com/d607864c04eecd7f447fc51dd88162a8cac3d690b6ccb7fe4795781b385c5487/68747470733a2f2f7472617669732d63692e6f72672f64746b61686c2f7068702d61727261792d746f6f6c732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/dtkahl/php-array-tools)

PHP array tools
===============

[](#php-array-tools)

Different tools for arrays in PHP.

#### Dependencies

[](#dependencies)

- `PHP >= 5.6.0`

#### Installation

[](#installation)

Install with [Composer](http://getcomposer.org):

```
composer require dtkahl/php-array-tools

```

Map Class
---------

[](#map-class)

This class provides a wrapper class for **indexed** arrays and implements `\ArrayAccess`, `\Countable` and `\Serializable` Interfaces.

### Usage

[](#usage)

Refer namespace

```
use Dtkahl\ArrayTools\Map;

```

create new Map

```
$map = new Map([
    'first_name' => 'Jeffrey',
    'last_name' => 'Clarence',
    'age' =>  24
]);
```

**available options:**

- `recursive` -&gt; create instance recursive (automatically create Map or Collection instance from array values)
- `key_locked` -&gt; only allow keys which are already defined in Map.

### Methods

[](#methods)

#### `get($key, $default = null)`

[](#getkey-default--null)

Returns property value by given key or returns `$default` if property does not exist.

#### `has($key)`

[](#haskey)

Determine if an property with given key exists in the map.

#### `set($key, $value)`

[](#setkey-value)

Set property (override if existing). Returns map instance.

#### `remove($key)`

[](#removekey)

Remove property if existing. Returns map instance.

#### `only()`

[](#only)

returns Map with all items with given keys.

#### `except()`

[](#except)

returns Map with all items except with given keys.

#### `toArray()`

[](#toarray)

Returns all items as array.

#### `toSerializedArray()`

[](#toserializedarray)

Returns serialized items (call `$item->toSerializedArray()` if item is object and has this method) as array.

#### `toJson()`

[](#tojson)

Returns all items of the map as JSON string.

#### `merge(array $data, $prefer_old = false)`

[](#mergearray-data-prefer_old--false)

Merge given array (or Map instance) into map data. If `$prefer_old` is true, the data will be merged in the opposite direction. Returns map instance.

#### `copy()`

[](#copy)

Returns clone of Map instance.

#### `clear()`

[](#clear)

Removes all items map and returns instance of map.

#### `each(\Closure $call)`

[](#eachclosure-call)

Walk through map items.

```
$map->each(function ($key, $value) {
  // do something
});
```

You can break by `return false` in the closure. Returns map instance.

#### `map(\Closure $call)`

[](#mapclosure-call)

Walk through map items and overrides there values by the return value of the given closure.

```
$map->map(function ($key, $value) {
  return "Mapped " . $value;
});
```

Returns collection instance.

Collection Class
----------------

[](#collection-class)

This class provides a wrapper class for **indexed** arrays implements `\ArrayAccess`, `\Countable`, `\Iterator` and `\Serializable` Interfaces.

**available options:**

- `recursive` -&gt; create instance recursive (automatically create Map or Collection instance from array values)

### Usage

[](#usage-1)

Refer namespace

```
use Dtkahl\ArrayTools\Collection;

```

create new Collection

```
$collection = new Collection([
    [
        'first_name' => 'Jeffrey',
        'last_name' => 'Clarence',
        'age' =>  24
    ],
    [
        'first_name' => 'Neil',
        'last_name' => 'Hiram',
        'age' =>  32
    ],
    [
        'first_name' => 'Derek',
        'last_name' => 'Deon',
        'age' =>  19
    ],
]);
```

### Methods

[](#methods-1)

#### `toArray()`

[](#toarray-1)

Returns all items of the collection as indexed array.

#### `toSerializedArray()`

[](#toserializedarray-1)

Returns serialized Items (call `$item->toSerializedArray()` if item is object and has this method) as array.

#### `toJson()`

[](#tojson-1)

Returns all items of the collection as JSON string.

#### `copy()`

[](#copy-1)

Returns clone of collection instance.

#### `getKeys()`

[](#getkeys)

Returns an array of collection item keys.

#### `hasKey(int $key)`

[](#haskeyint-key)

Determine if an item with given key exists in the collection.

#### `isEmpty()`

[](#isempty)

Determine if there are no items in the collection.

#### `getValues()`

[](#getvalues)

Returns an array of Collection items. (actually the same like `toArray()` because collection data is always an indexed array)

#### `hasValue(int $value)`

[](#hasvalueint-value)

Determine if an item with given value exists in the collection.

#### `count()`

[](#count)

Returns the count of items in the collection.

#### `clear()`

[](#clear-1)

Remove all item from the collection. Returns collection instance.

#### `get(int $key)`

[](#getint-key)

Returns the collection item with the given key.

#### `remove(int $key, bool $do_not_clear = false)`

[](#removeint-key-bool-do_not_clear--false)

Remove the collection item with the given key. Returns collection instance.

#### `each(\Closure $call)`

[](#eachclosure-call-1)

Walk through collection items.

```
$collection->each(function ($item, $key) {
  // do something
});
```

You can break by `return false` in the closure. Returns collection instance.

#### `filter(\Closure $call)`

[](#filterclosure-call)

Walk trough collection items and only keep items where closure returns true.

```
$collection->filter(function ($item, $key) {
  return $item['age'] > 10;
});
```

#### `reverse()`

[](#reverse)

Reverse the items in the collection. Returns collection instance.

#### `first()`

[](#first)

Returns the first collection item.

#### `last()`

[](#last)

Returns the last collection item.

#### `shift()`

[](#shift)

Returns and removes the first collection item.

#### `unshift(mixed $value)`

[](#unshiftmixed-value)

Push an item onto the beginning of the collection. Returns collection instance.

#### `pop()`

[](#pop)

Returns and removes the last collection item.

#### `push(mixed $value)`

[](#pushmixed-value)

Push an item onto the ending of the collection. Returns collection instance.

#### `put(int $key, mixed $value)`

[](#putint-key-mixed-value)

Put an item in the collection by key. (Override if existing) Returns collection instance.

#### `inject(int $key, mixed $value)`

[](#injectint-key-mixed-value)

Put an item in the collection by key. (move items one possition up where key &gt;= given key) Returns collection instance.

#### `merge(array $array)`

[](#mergearray-array)

Merge given array (or Collection instance) into collection data. Returns collection instance.

#### `sort(\Closure $call)`

[](#sortclosure-call)

Sorts the collection items with [usort](http://php.net/manual/en/function.usort.php)

```
$collection->sort(function ($a, $b) {
  return $a['age'] > $b['age'];
});
```

Returns collection instance.

#### `map(\Closure $call)`

[](#mapclosure-call-1)

Walk through collection items and overrides them by the return value of the given closure.

```
$collection->map(function ($item) {
  return $item['first_name] . " " . $item['last_name];
});
```

Returns collection instance.

#### `slice(int $offset, int|null $length = null)`

[](#sliceint-offset-intnull-length--null)

Slize the collection data with [array\_slice](http://php.net/manual/en/function.array-slice.php). Returns collection instance.

#### `chunk(int $size)`

[](#chunkint-size)

Returns an array of collections with given chunk size.

#### `current(int $size)`

[](#currentint-size)

Returns item on current pointer position or 'null' if there is no item.

#### `next(int $size)`

[](#nextint-size)

Increase internal pointer by one and returns the item or 'null' if there is no item on this position.

```
while ($item = $collection->next()) {
  var_dump($item['age']);
  echo "";
}
```

#### `previous(int $size)`

[](#previousint-size)

Decrease internal pointer by one and returns the item or 'null' if there is no item on this position.

#### `setPointer(int $pointer)`

[](#setpointerint-pointer)

set the internal pointer position to the given value.

#### `lists(array $keys)`

[](#listsarray-keys)

returns an array with given array entries/public properties of collection items.

```
$collection->lists(['age']) // array (array('age'=>24'), array('age'=>32), array('age'=>19))
```

#### `join(string $glue)`

[](#joinstring-glue)

Join collection items with a given $glue.

#### `unique()`

[](#unique)

Remove duplicated entries. Returns collection instance.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance4

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~20 days

Recently: every ~107 days

Total

28

Last Release

3156d ago

Major Versions

1.0.4 → 2.0.02016-03-22

2.10 → 3.0.02017-03-24

PHP version history (2 changes)1.0.0PHP &gt;=7.0.0

1.0.3PHP &gt;=5.6.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/3c8f601e8ceab86287cd0d97b5a749fd51c9aea595f351333abf5e6e70930e95?d=identicon)[dtkahl](/maintainers/dtkahl)

---

Top Contributors

[![dantodev](https://avatars.githubusercontent.com/u/13280775?v=4)](https://github.com/dantodev "dantodev (90 commits)")

---

Tags

arraysphpwrapper

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/dtkahl-php-array-tools/health.svg)

```
[![Health](https://phpackages.com/badges/dtkahl-php-array-tools/health.svg)](https://phpackages.com/packages/dtkahl-php-array-tools)
```

PHPackages © 2026

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