2017 © Pedro Peláez
 

library php-array-tools

image

dtkahl/php-array-tools

  • Tuesday, September 26, 2017
  • by dtkahl
  • Repository
  • 0 Watchers
  • 4 Stars
  • 728 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 0 Forks
  • 7 Open issues
  • 30 Versions
  • 2 % Grown

The README.md

Latest Stable Version License Build Status, (*1)

PHP array tools

Different tools for arrays in PHP., (*2)

Dependencies

  • PHP >= 5.6.0

Installation

Install with Composer:, (*3)

composer require dtkahl/php-array-tools

Map Class

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

Usage

Refer namespace, (*5)

use Dtkahl\ArrayTools\Map;

create new Map, (*6)

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

available options:, (*7)

  • recursive -> create instance recursive (automatically create Map or Collection instance from array values)
  • key_locked -> only allow keys which are already defined in Map.

Methods

get($key, $default = null)

Returns property value by given key or returns $default if property does not exist., (*8)

has($key)

Determine if an property with given key exists in the map., (*9)

set($key, $value)

Set property (override if existing). Returns map instance., (*10)

remove($key)

Remove property if existing. Returns map instance., (*11)

only()

returns Map with all items with given keys., (*12)

except()

returns Map with all items except with given keys., (*13)

toArray()

Returns all items as array., (*14)

toSerializedArray()

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

toJson()

Returns all items of the map as JSON string., (*16)

merge(array $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., (*17)

copy()

Returns clone of Map instance., (*18)

clear()

Removes all items map and returns instance of map., (*19)

each(\Closure $call)

Walk through map items., (*20)

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

You can break by return false in the closure. Returns map instance., (*21)

map(\Closure $call)

Walk through map items and overrides there values by the return value of the given closure., (*22)

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

Returns collection instance., (*23)

Collection Class

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

available options:, (*25)

  • recursive -> create instance recursive (automatically create Map or Collection instance from array values)

Usage

Refer namespace, (*26)

use Dtkahl\ArrayTools\Collection;

create new Collection, (*27)

$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

toArray()

Returns all items of the collection as indexed array., (*28)

toSerializedArray()

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

toJson()

Returns all items of the collection as JSON string., (*30)

copy()

Returns clone of collection instance., (*31)

getKeys()

Returns an array of collection item keys., (*32)

hasKey(int $key)

Determine if an item with given key exists in the collection., (*33)

isEmpty()

Determine if there are no items in the collection., (*34)

getValues()

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

hasValue(int $value)

Determine if an item with given value exists in the collection., (*36)

count()

Returns the count of items in the collection., (*37)

clear()

Remove all item from the collection. Returns collection instance., (*38)

get(int $key)

Returns the collection item with the given key., (*39)

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

Remove the collection item with the given key. Returns collection instance., (*40)

each(\Closure $call)

Walk through collection items., (*41)

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

You can break by return false in the closure. Returns collection instance., (*42)

filter(\Closure $call)

Walk trough collection items and only keep items where closure returns true., (*43)

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

reverse()

Reverse the items in the collection. Returns collection instance., (*44)

first()

Returns the first collection item., (*45)

last()

Returns the last collection item., (*46)

shift()

Returns and removes the first collection item., (*47)

unshift(mixed $value)

Push an item onto the beginning of the collection. Returns collection instance., (*48)

pop()

Returns and removes the last collection item., (*49)

push(mixed $value)

Push an item onto the ending of the collection. Returns collection instance., (*50)

put(int $key, mixed $value)

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

inject(int $key, mixed $value)

Put an item in the collection by key. (move items one possition up where key >= given key) Returns collection instance., (*52)

merge(array $array)

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

sort(\Closure $call)

Sorts the collection items with usort, (*54)

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

Returns collection instance., (*55)

map(\Closure $call)

Walk through collection items and overrides them by the return value of the given closure., (*56)

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

Returns collection instance., (*57)

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

Slize the collection data with array_slice. Returns collection instance., (*58)

chunk(int $size)

Returns an array of collections with given chunk size., (*59)

current(int $size)

Returns item on current pointer position or 'null' if there is no item., (*60)

next(int $size)

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

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

previous(int $size)

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

setPointer(int $pointer)

set the internal pointer position to the given value., (*63)

lists(array $keys)

returns an array with given array entries/public properties of collection items., (*64)

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

join(string $glue)

Join collection items with a given $glue., (*65)

unique()

Remove duplicated entries. Returns collection instance., (*66)

The Versions

26/09 2017

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

26/09 2017

3.1.1

3.1.1.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

17/08 2017

3.1.0

3.1.0.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

18/06 2017

3.0.1

3.0.1.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

25/03 2017

3.x-dev

3.9999999.9999999.9999999-dev

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

25/03 2017

3.0.0

3.0.0.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

23/07 2016

2.10

2.10.0.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

23/07 2016

2.9.0

2.9.0.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

20/05 2016

2.8.0

2.8.0.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

12/04 2016

2.7.2

2.7.2.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

12/04 2016

2.7.1

2.7.1.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

12/04 2016

2.7.0

2.7.0.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

30/03 2016

2.6.1

2.6.1.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

30/03 2016

2.6.0

2.6.0.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

30/03 2016

2.5.0

2.5.0.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

25/03 2016

2.4.0

2.4.0.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

25/03 2016

2.3.1

2.3.1.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

25/03 2016

2.3.0

2.3.0.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

25/03 2016

2.2.1

2.2.1.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

24/03 2016

2.2.0

2.2.0.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

24/03 2016

2.1.1

2.1.1.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

24/03 2016

2.1.0

2.1.0.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

24/03 2016

2.0.2

2.0.2.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

24/03 2016

2.0.1

2.0.1.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

22/03 2016

2.0.0

2.0.0.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

20/03 2016

1.0.4

1.0.4.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

19/03 2016

1.0.3

1.0.3.0

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

15/03 2016

1.0.2

1.0.2.0

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

15/03 2016

1.0.1

1.0.1.0

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

15/03 2016

1.0.0

1.0.0.0

  Sources   Download

MIT

The Requires

  • php >=7.0.0