Recommend this page to a friend! |
Download .zip |
Info | Documentation | View files (8) | Download .zip | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2022-08-13 (1 month ago) | Not yet rated by the users | Total: 31 This week: 1 | All time: 10,599 This week: 206 |
Version | License | PHP version | Categories | |||
dotarray 1.0 | MIT/X Consortium ... | 5.6 | PHP 5, Data types |
Description | Author | |
This class can access arrays using the dot notation text keys. |
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');
composer require xobotyi/dotarray
{
"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')
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 =)
___
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.
___
A::walk(array $array, callable $callback [, bool $recursive = false])
_Description:_ Applies $callback to each element of the $array.
___
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.
___
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.
___
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.
___
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 |
File | Role | Description | ||
---|---|---|---|---|
benchmarks (1 file) | ||||
src (1 file) | ||||
tests (1 file) | ||||
.travis.yml | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpunit.xml.dist | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.