|
| 1 | +# Array Extensions |
| 2 | + |
| 3 | +This is a collection of array wrappers. |
| 4 | + |
| 5 | +## Composite Key Array |
| 6 | + |
| 7 | +Sometimes it is useful to have ability to access nested value with one, |
| 8 | +array like, key (some questions were asked about this: [on quora](https://www.quora.com/Learning-PHP-Is-there-a-way-to-get-the-value-of-multi-dimensional-array-by-specifying-the-key-with-a-variable), [on stackoverflow](http://stackoverflow.com/questions/22614817/get-a-value-from-a-multidimensional-array-using-the-dot-syntax)). |
| 9 | + |
| 10 | +`CompositeKeyArray` class gives you the ability to do all basic array operations using array-like (nested) key. |
| 11 | +It implemets [`ArrayAccess`](http://php.net/manual/en/class.arrayaccess.php) interface: |
| 12 | + |
| 13 | +### 1. offsetExists: |
| 14 | + |
| 15 | +You can check nested keys for existance. |
| 16 | + |
| 17 | +```php |
| 18 | + $array = new CompositeKeyArray([ |
| 19 | + 'foo' => [ |
| 20 | + 'bar' => 'baz' |
| 21 | + ] |
| 22 | + ]); |
| 23 | + |
| 24 | + var_dump(isset($array[['foo', 'bar']])); // => bool(true) |
| 25 | + var_dump(isset($array[['foo', 'quux']])); // => bool(false) |
| 26 | +``` |
| 27 | + |
| 28 | +### 2. offsetGet: |
| 29 | + |
| 30 | +You can get value by nested key. If nested key is not set the `UndefinedOffsetException` will be thrown. |
| 31 | + |
| 32 | +```php |
| 33 | + $array = new CompositeKeyArray([ |
| 34 | + 'foo' => [ |
| 35 | + 'bar' => 'baz' |
| 36 | + ] |
| 37 | + ]); |
| 38 | + |
| 39 | + var_dump($array[['foo', 'bar']]); // => string(3) "baz" |
| 40 | + var_dump($array[['foo', 'quux']]); // => PHP Fatal error: Uncaught UndefinedOffsetException: Undefined offset quux. |
| 41 | +``` |
| 42 | + |
| 43 | +### 3. offsetSet: |
| 44 | + |
| 45 | +You can set value for nested key. |
| 46 | + |
| 47 | +```php |
| 48 | + $array = new CompositeKeyArray(); |
| 49 | + |
| 50 | + $array[['foo', 'bar']] = 'baz'; |
| 51 | + |
| 52 | + var_dump($array[['foo', 'bar']]); // => string(3) "baz" |
| 53 | +``` |
| 54 | + |
| 55 | +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`. |
| 56 | +The reason was explained [here](http://stackoverflow.com/questions/20053269/indirect-modification-of-overloaded-element-of-splfixedarray-has-no-effect). So in order to achive the desired result you have to do the following: |
| 57 | + |
| 58 | +```php |
| 59 | + $array = new CompositeKeyArray([ |
| 60 | + 'foo' => [] |
| 61 | + ]); |
| 62 | + |
| 63 | + $array['foo']['bar'] = 'baz'; // => PHP Notice: Indirect modification of overloaded element of CompositeKeyArray has no effect |
| 64 | + |
| 65 | + var_dump($array['foo']); // => array(0) {} |
| 66 | + |
| 67 | + $array[['foo', 'bar']] = 'baz'; |
| 68 | + |
| 69 | + var_dump($array['foo']); // => array(1) {["bar"] => string(3) "baz"} |
| 70 | +``` |
| 71 | + |
| 72 | +But there is another edge case left: when you need to append element at the end of an array. |
| 73 | + |
| 74 | +```php |
| 75 | + $array = new CompositeKeyArray([ |
| 76 | + 'foo' => [] |
| 77 | + ]); |
| 78 | + |
| 79 | + $array[[[]]] = 'bar'; |
| 80 | + $array[['foo', []]] = 'baz'; |
| 81 | + $array[['foo', []]] = 'qux'; |
| 82 | + |
| 83 | + var_dump($array->toArray()); |
| 84 | + |
| 85 | + // => array(2) { |
| 86 | + // ["foo"]=> |
| 87 | + // array(2) { |
| 88 | + // [0]=> |
| 89 | + // string(3) "baz" |
| 90 | + // [1]=> |
| 91 | + // string(3) "qux" |
| 92 | + // } |
| 93 | + // [0]=> |
| 94 | + // string(3) "bar" |
| 95 | + // } |
| 96 | + |
| 97 | +``` |
| 98 | + |
| 99 | +### 4. offsetUnset: |
| 100 | + |
| 101 | +You can unset nested key. |
| 102 | + |
| 103 | +```php |
| 104 | + $array = new CompositeKeyArray([ |
| 105 | + 'foo' => [ |
| 106 | + 'bar' => 'baz' |
| 107 | + ] |
| 108 | + ]); |
| 109 | + |
| 110 | + unset($array[['foo', 'bar']]); |
| 111 | + |
| 112 | + var_dump($array['foo']); // => array(0) {} |
| 113 | +``` |
| 114 | + |
| 115 | +After nested manipulations you might want to get back the **real array**. This can be done by calling `$array->toArray()`. |
0 commit comments