Skip to content

vayes/arr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Arr - Array Utilities for PHP

ArrayDot

ArrayDot provides an easy access to arrays of data with dot notation in a lightweight and fast way. Inspired by Laravel Collection.

ArrayDot implements PHP's ArrayAccess interface and ArrayDot object can also be used the same way as normal arrays with additional dot notation.

Examples

With ArrayDot you can change this regular array syntax:

$array['info']['home']['address'] = 'Kings Square';

echo $array['info']['home']['address'];

// Kings Square

to this (ArrayDot object):

$dot->set('info.home.address', 'Kings Square');

echo $dot->get('info.home.address');

or even this (ArrayAccess):

$dot['info.home.address'] = 'Kings Square';

echo $dot['info.home.address'];

Install

Install the latest version using Composer:

$ composer require vayes/arr

Not available via composer yet.

Usage

Create a new ArrayDot object:

$dot = new Vayes\Arr\ArrayDot;

// With existing array
$dot = new Vayes\Arr\ArrayDot($array);

Methods

ArrayDot has the following methods:

add()

Sets a given key / value pair if the key doesn't exist already:

$dot->add('user.name', 'John');

// Equivalent vanilla PHP
if (!isset($array['user']['name'])) {
    $array['user']['name'] = 'John';
}

Multiple key / value pairs:

$dot->add([
    'user.name' => 'John',
    'page.title' => 'Home'
]);

all()

Returns all the stored items as an array:

$values = $dot->all();

clear()

Deletes the contents of a given key (sets an empty array):

$dot->clear('user.settings');

// Equivalent vanilla PHP
$array['user']['settings'] = [];

Multiple keys:

$dot->clear(['user.settings', 'app.config']);

All the stored items:

$dot->clear();

// Equivalent vanilla PHP
$array = [];

count()

Returns the number of items in a given key:

$dot->count('user.siblings');

Items in the root of ArrayDot object:

$dot->count();

// Or use coun() function as ArrayDot implements Countable
count($dot);

delete()

Deletes the given key:

$dot->delete('user.name');

// ArrayAccess
unset($dot['user.name']);

// Equivalent vanilla PHP
unset($array['user']['name']);

Multiple keys:

$dot->delete([
    'user.name',
    'page.title'
]);

flatten()

Returns a flattened array with the keys delimited by a given character (default "."):

$flatten = $dot->flatten();

get()

Returns the value of a given key:

echo $dot->get('user.name');

// ArrayAccess
echo $dot['user.name'];

// Equivalent vanilla PHP < 7.0
echo isset($array['user']['name']) ? $array['user']['name'] : null;

// Equivalent vanilla PHP >= 7.0
echo $array['user']['name'] ?? null;

Returns a given default value, if the given key doesn't exist:

echo $dot->get('user.name', 'some default value');

has()

Checks if a given key exists (returns boolean true or false):

$dot->has('user.name');

// ArrayAccess
isset($dot['user.name']);

Multiple keys:

$dot->has([
    'user.name',
    'page.title'
]);

isEmpty()

Checks if a given key is empty (returns boolean true or false):

$dot->isEmpty('user.name');

// ArrayAccess
empty($dot['user.name']);

// Equivalent vanilla PHP
empty($array['user']['name']);

Multiple keys:

$dot->isEmpty([
    'user.name',
    'page.title'
]);

Checks the whole ArrayDot object:

$dot->isEmpty();

merge()

Merges a given array or another ArrayDot object:

$dot->merge($array);

// Equivalent vanilla PHP
array_merge($originalArray, $array);

Merges a given array or another ArrayDot object with the given key:

$dot->merge('user', $array);

// Equivalent vanilla PHP
array_merge($originalArray['user'], $array);

mergeRecursive()

Recursively merges a given array or another ArrayDot object:

$dot->mergeRecursive($array);

// Equivalent vanilla PHP
array_merge_recursive($originalArray, $array);

Recursively merges a given array or another ArrayDot object with the given key:

$dot->mergeRecursive('user', $array);

// Equivalent vanilla PHP
array_merge_recursive($originalArray['user'], $array);

mergeRecursiveDistinct()

Recursively merges a given array or another ArrayDot object. Duplicate keys overwrite the value in the original array (unlike mergeRecursiveDistinct(), where duplicate keys are transformed into arrays with multiple values):

$dot->mergeRecursiveDistinct($array);

Recursively merges a given array or another ArrayDot object with the given key. Duplicate keys overwrite the value in the original array.

$dot->mergeRecursiveDistinct('user', $array);

pull()

Returns the value of a given key and deletes the key:

echo $dot->pull('user.name');

// Equivalent vanilla PHP < 7.0
echo isset($array['user']['name']) ? $array['user']['name'] : null;
unset($array['user']['name']);

// Equivalent vanilla PHP >= 7.0
echo $array['user']['name'] ?? null;
unset($array['user']['name']);

Returns a given default value, if the given key doesn't exist:

echo $dot->pull('user.name', 'some default value');

Returns all the stored items as an array and clears the ArrayDot object:

$items = $dot->pull();

push()

Pushes a given value to the end of the array in a given key:

$dot->push('users', 'John');

// Equivalent vanilla PHP
$array['users'][] = 'John';

Pushes a given value to the end of the array:

$dot->push('John');

// Equivalent vanilla PHP
$array[] = 'John';

replace()

Replaces the values with values having the same keys in the given array or ArrayDot object:

$dot->replace($array);

// Equivalent vanilla PHP
array_replace($originalArray, $array);

Replaces the values with values having the same keys in the given array or ArrayDot object with the given key:

$dot->merge('user', $array);

// Equivalent vanilla PHP
array_replace($originalArray['user'], $array);

replace() is not recursive.

set()

Sets a given key / value pair:

$dot->set('user.name', 'John');

// ArrayAccess
$dot['user.name'] = 'John';

// Equivalent vanilla PHP
$array['user']['name'] = 'John';

Multiple key / value pairs:

$dot->set([
    'user.name' => 'John',
    'page.title'     => 'Home'
]);

setArray()

Replaces all items in ArrayDot object with a given array:

$dot->setArray($array);

setReference()

Replaces all items in ArrayDot object with a given array as a reference and all future changes to ArrayDot will be made directly to the original array:

$dot->setReference($array);

toJson()

Returns the value of a given key as JSON:

echo $dot->toJson('user');

Returns all the stored items as JSON:

echo $dot->toJson();

ArrayStack

Usage:

$stack = new ArrayStack($array);

Now you have access to:

# Now you have access to:
public function getData(): array
public function getMeta(): ArrayMeta
public function loop(\Closure $closure)

If you call getMeta(), then you have access to:

public function getCount(): int
public function getFirstIndex(): int
public function getFirstKey()
public function getFirstValue()
public function getLastIndex(): int
public function getLastKey()
public function getLastValue()
public function offsetSet($offset, $value)
public function offsetExists($offset)
public function offsetUnset($offset)
public function offsetGet($offset)

License

MIT license