PHP Classes

PHP Dot Notation to Array: Access arrays using the dot notation text keys

Recommend this page to a friend!
  Info   View files Documentation   View files View files (8)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2022-08-13 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 31 This week: 1All time: 10,599 This week: 206Up
Version License PHP version Categories
dotarray 1.0MIT/X Consortium ...5.6PHP 5, Data types
Description Author

This class can access arrays using the dot notation text keys.

It provides a class that can store an array and functions to access the array elements using text string parameters that identify the indexes of the array elements.

Currently, it can:

- Get individual elements with a given dot path

- Get all elements of the array

- Get the last or first element of the array

- Go through all elements of the array and invoke a given callback function, passing each array element as a parameter

- Check elements of the array to verify if they pass a test using a given test function

- Check if the array has specific values or keys

- Check if the array is associative

- Check if the array has values in numeric sequence

- Set the values of array elements with a given dot notation path

- Append or prepend new array values

- Delete array elements with given dot notation paths

- Create array chunks with a limited number of elements

- Flip the order of the given array of elements

- Change the case of array elements

- Get an array that has the elements that are different or the same between two arrays

- Etc.

Picture of Chun-Sheng, Li
  Performance   Level  
Name: Chun-Sheng, Li <contact>
Classes: 10 packages by
Country: Taiwan Taiwan
Innovation award
Innovation award
Nominee: 3x

Details

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.

  Files folder image Files  
File Role Description
Files folder imagebenchmarks (1 file)
Files folder imagesrc (1 file)
Files folder imagetests (1 file)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  benchmarks  
File Role Description
  Accessible without login Plain text file benchmark.php Example Example script

  Files folder image Files  /  src  
File Role Description
  Plain text file A.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file ATest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:31
This week:1
All time:10,599
This week:206Up
For more information send a message to info at phpclasses dot org.