PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Chun-Sheng, Li   PHP Dot Notation to Array   README.md   Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: PHP Dot Notation to Array
Access arrays using the dot notation text keys
Author: By
Last change:
Date: 1 year ago
Size: 9,464 bytes
 

Contents

Class file image Download

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 * xobotyi\A * Getting * Iteration * Checking * Modification * Interaction * Others

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.