Download the PHP package thorough-php/arrays without Composer
On this page you can find all versions of the php package thorough-php/arrays. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download thorough-php/arrays
More information about thorough-php/arrays
Files in thorough-php/arrays
Package arrays
Short Description This is a collection of array wrappers
License MIT
Informations about the package arrays
Array Extensions
This is a collection of array wrappers.
Table of Contents =================
- Composite Key Array
- 1. offsetExists
- 2. offsetGet
- 3. offsetSet
- 4. offsetUnset
- XPath Key Array
- Dotted Key Array
- One-off Array
- Write-once Array
Composite Key Array
Sometimes it is useful to have ability to access nested value with one, array like, key (some questions were asked about this: on quora, on stackoverflow).
CompositeKeyArray class gives you the ability to do all basic array operations using array-like (nested) key.
It implemets ArrayAccess interface:
1. offsetExists:
You can check nested keys for existance.
2. offsetGet:
You can get value by nested key. If nested key is not set the UndefinedOffsetException will be thrown.
3. offsetSet:
You can set value for nested key.
There is one pitfall. When you try to do $array['foo']['bar'] = 'baz' you get Indirect modification of overloaded element of CompositeKeyArray has no effect.
The reason was explained here. So in order to achive the desired result you have to do the following:
But there is another edge case left: when you need to append element at the end of an array.
4. offsetUnset:
You can unset nested key.
After nested manipulations you might want to get back the real array. This can be done by calling $array->toArray().
XPath Key Array
This is not a real xpath! This class instead of array-like key users string of keys delimited with /.
This one was inspired by an old article.
Compared to CompositeKeyArray, XPathKeyArray has some limitations:
- You cannot use keys with
/in them. - You cannot use
nullas key.
Dotted Key Array
This class instead of array-like key users string of keys delimited with ..
Compared to CompositeKeyArray, DottedKeyArray has some limitations:
- You cannot use keys with
.in them. - You cannot use
nullas key.
One-off Array
Sometimes you want to get value from an array by key and unset this key after that. The OneOffArray class helps you with this.
Again this class can be used in combination with CompositeKeyArray or its descendents: XPathKeyArray or DottedKeyArray.
Actually, it can be used in combination with any object that implemets ArrayAccess.
Write-once Array
If you want to be sure that each offset in your array would be written only once you can use WriteOnceArray. If you try to set one particular offset more than one time IllegalOffsetException will be thrown:
Because offsetExists method is used in order to ensure write-once behaviour, offsetUnset method call is illegal: