xobotyi/dotarray

Handle your arrays easily with dot notation access style

v1.1.0 2017-12-12 18:45 UTC

README

Build Status Codacy Badge Codacy Badge PHP 7 ready Latest Stable Version Total Downloads License

Haven't you thought, that using $array['some']['nested']['array']['value'] is to long and hard at least to write? Well, i did. And made this lib which will help you to manage your arrays in dot-notation style.

This lib will help you to do this:

$var = $array['a']['b']['c']['d'] ?? 'default value';

a bit shorter:

$var = A::get($array, 'a.b.c.d', 'default value');

Contents

  1. Requirements
  2. Installation
  3. Classes and methods

Requrements

Installation

composer require

composer require xobotyi/dotarray

composer.json

{
    "require": {
      "xobotyi/dotarray":"^1.0.8"
    }
}

After that run composer update or php composer.phar update, and you will be able to A::set($arrays, 'for.some.dotted.magic')

Classes and methods

xobotyi\A

A is class containing static methods to perform actions on common arrays and ArrayObjects. Someone will think that A is bad or inconvenient name for class, but i think that it's handy to type A:: without releasing Shift button =)

Array getting

  • get - Get an element.
  • values - Get all values.
  • last - Get last element(s).
  • lastKeys - Get key(s) of last elements(s).
  • first - Get first element(s).
  • firstKeys - Get key(s) of first elements(s).
A::get(array $array [, ?string $path = null [, $default = null]])

Description: Returns $array's value placed on the $path or $default value if provided $path doesn't exists.

A::values(array $array [, bool $flatten = false])

Description: Returns all the values from the $array as a sequential array (without it's keys).

A::last(array $array [, int $count = 1])

Description: Returns last $count element(s) value(s) from the $array.

A::lastKeys(array $array [, int $count = 1])

Description: Returns last $count element(s) key(s) from the $array.

A::first(array $array [, int $count = 1])

Description: Returns first $count element(s) value(s) from the $array.

A::firstKeys(array $array [, int $count = 1])

Description: Returns first $count element(s) keys from the $array.

Array iteration

  • walk - Execute a provided function once for each array element
A::walk(array $array, callable $callback [, bool $recursive = false])

Description: Applies $callback to each element of the $array.

Array checking

  • every - Test whether all elements in the array pass the test implemented by the provided function.
  • any - Test whether at least one element in the array passes the test implemented by the provided function.
  • has - Check whether all provided values presented in array.
  • hasAny - Test whether at least one provided value presented in array.
  • hasKey - Check whether all provided values presented in array as key.
  • hasAnyKey - Check whether at least one provided value presented in array as key.
  • arrayKeyExists - Check whether array has provided key.
  • isAssoc - Check whether array is associative, e.g. has string keys.
  • isSequential - Check whether array is sequential, e.g. has only int keys placed in ascending order.
A::every(array $array, callable $callback)

Description: Applies $callback to each $array's element and returns true if EVERY call returned TRUE.

A::any(array $array, callable $callback)

Description: Applies $callback to each $array's element and returns true if ANY call returned TRUE.

A::has(array $array, ...$values)

Description: Tests whether $array contains ALL of provided ...$values.

A::hasAny(array $array, ...$values)

Description: Tests whether $array contains ANY of provided ...$values.

A::hasKey(array $array, string ...$paths)

Description: Tests whether $array has ALL of provided ...paths.

A::hasAnyKey(array $array, string ...$paths)

Description: Tests whether $array has ANY of provided ...paths.

A::arrayKeyExists(array &$array, string $key)

Description: The faster analog to \array_key_exists().

A::isAssoc(array $array)

Description: Tests whether $array is an associative array.

A::isSequential(array $array)

Description: Tests whether $array is a sequential ([1,2,3,4,...]) array.

Array modification

  • set - Set value(s) with provided keys(s).
  • append - Add element(s) to the end of array.
  • prepend - Add element(s) to the beginning of array.
  • delete - Delete an element with provided keys(s).
  • chunk - Split array into a chunks of provided size.
  • flip - Swap keys and values.
  • changeKeyCase - Change the case of all keys in an array.
A::set(array $array, string|array $path [, $value])

Description: Sets the $value on the $path.
If $path parameter is an array - it's keys will be used as paths and vales as values.

A::append(array $array, ...$values)

Description: Adds passed ...$values to the end of $array.

A::prepend(array $array, ...$values)

Description: Adds passed ...$values to the beginning of $array.

A::delete(array $array, string ...$paths)

Description: Deletes $array's items placed on the provided ...$paths.

A::chunk(array $array, int $chunkSize [, bool $preserveKeys = false])

Description: Chunks an array into arrays with $chunkSize elements. The last chunk may contain less than $chunkSize elements.

A::flip(array $array)

Description: Returns an array in flip order, i.e. keys from array become values and values from array become keys.
If a value has several occurrences, the latest key will be used as its value, and all others will be lost.

A::changeKeyCase(array $array [, int $case = CASE_LOWER [, bool $recursive = false]])

Description: Returns an array with all keys from $array lower- or upper- cased.

Arrays interaction

  • diff - Compute right diff of provided arrays.
  • symdiff - Compute symmetric diff of provided arrays.
  • diffAssoc - Compute intersection of provided arrays with additional index check.
  • intersect - Compute intersection of provided arrays.
  • intersectAssoc - Compute intersection of provided arrays with additional index check.
A::diff(array $array, array ...$arrays [, bool $preserveKeys = false])

Description: Compares $array against ...$arrays and returns the values in $array that are not present in any of the other ...$arrays.
If $preserveKeys set to TRUE values keys will be preserved.

A::symdiff(array $array, array ...$arrays [, bool $softDiff = false])

Description: Returns symmetric difference between arrays (values not presented in all the arrays simultaneously).
If $softDiff is set to TRUE, result will include only values that has no intersection with other arrays.

A::diffAssoc(array $array, array ...$arrays)

Description: Acts like A::diff() but the array keys are also used in the comparison.

A::intersect(array $array, array ...$arrays [, bool $preserveKeys = false])

Description: Compares $array against ...$arrays and returns all the values of $array that are present in all ...$arrays. If $preserveKeys set to TRUE values keys will be preserved.

A::intersectAssoc(array $array, array ...$arrays)

Description: Acts like A::intersect() but the array keys are also used in the comparison.

Array Others

  • glue - Glue array with provided delimiter.
  • splitPath - Split provided string into an array representing nested keys.
A::splitPath(string $path)

Description: Splits given string to it's segments according to dot notation.

A::glue(array $array, string $glue = '')

Description: Glues $array items into a string, with $glue as delimiter.