22Easy access to multidimensional arrays with dot notation.
33With dot notation, your code is cleaner and handling deeper arrays is super easy.
44
5- This class implements PHP's ArrayAccess class, so Dot object can also be used same way as normal arrays but with dot notation.
5+ This class implements PHP's ArrayAccess class, so Dot object can also be used the same way as normal arrays with additional dot notation.
66
77With Dot you can change this:
88
@@ -32,6 +32,17 @@ composer require adbario/php-dot-notation
3232
3333Or just copy the class file Dot.php and handle namespace yourself.
3434
35+ #### With [ Composer] ( https://getcomposer.org/ ) :
36+
37+ ```
38+ composer require adbario/php-dot-notation
39+ ```
40+
41+ #### Manual installation:
42+ 1 . Download the latest release
43+ 2 . Extract the files into your project
44+ 3 . require_once '/path/to/php-dot-notation/src/Dot.php';
45+
3546## Usage
3647
3748This array will be used as a reference on this guide:
@@ -55,35 +66,35 @@ $array = [
5566];
5667```
5768
58- ### Create Dot object
69+ ### Create a Dot object
5970
60- To start without any data , just create a new Dot object:
71+ To start with an empty array , just create a new Dot object:
6172
6273``` php
63- $data = new \AdBar \Dot;
74+ $data = new \Adbar \Dot;
6475```
6576
66- If you have an array already available, inject it to Dot object:
77+ If you have an array already available, inject it to the Dot object:
6778
6879``` php
69- $data = new \AdBar \Dot($array);
80+ $data = new \Adbar \Dot($array);
7081```
7182
72- Set data after creating Dot object:
83+ Set an array after creating the Dot object:
7384
7485``` php
75- $data->setData ($array);
86+ $data->setArray ($array);
7687```
7788
78- Set data as a reference, and all changes will be made directly to original array:
89+ Set an array as a reference, and all changes will be made directly to the original array:
7990
8091``` php
81- $data->setDataAsRef ($array);
92+ $data->setReference ($array);
8293```
8394
84- ### Set value
95+ ### Set a value
8596
86- Set i.e. phone number in 'home' array:
97+ Set i.e. a phone number in the 'home' array:
8798
8899``` php
89100$data->set('info.home.tel', '09-123-456-789');
@@ -101,21 +112,39 @@ $data->set([
101112]);
102113```
103114
104- If value already exists, Dot will override it with new value.
115+ If the value already exists, Dot will override it with a new value.
105116
106- ### Get value
117+ ### Get a value
107118
108119``` php
109120echo $data->get('info.home.address');
110121
111- // Default value if path doesn't exist
122+ // Default value if the path doesn't exist
112123echo $data->get('info.home.country', 'some default value');
113124
114125// Array style
115126echo $data['info.home.address'];
116127```
117128
118- ### Add value
129+ Get all the stored values:
130+
131+ ``` php
132+ $values = $data->all();
133+ ``
134+
135+ Get a value from a path and remove it:
136+
137+ ```php
138+ $address = $data->pull('home.address');
139+ ```
140+
141+ Get all the stored values and remove them:
142+
143+ ``` php
144+ $values = $data->pull();
145+ ```
146+
147+ ### Add a value
119148
120149``` php
121150$data->add('info.kids', 'Amy');
@@ -129,7 +158,7 @@ $data->add('info.kids', [
129158]);
130159```
131160
132- ### Check if value exists
161+ ### Check if a value exists
133162
134163``` php
135164if ($data->has('info.home.address')) {
@@ -142,7 +171,7 @@ if (isset($data['info.home.address'])) {
142171}
143172```
144173
145- ### Delete value
174+ ### Delete a value
146175
147176``` php
148177$data->delete('info.home.address');
@@ -161,18 +190,12 @@ $data->delete([
161190
162191### Clear values
163192
164- Delete all values from path:
193+ Delete all the values from a path:
165194
166195``` php
167196$data->clear('info.home');
168197```
169198
170- If path doesn't exist, create an empty array on it
171-
172- ``` php
173- $data->clear('info.home.rooms', true);
174- ```
175-
176199Clear multiple paths at once:
177200
178201``` php
@@ -187,32 +210,54 @@ Clear all data:
187210$data->clear();
188211```
189212
213+ ### Sort the values
214+
215+ You can sort the values of a given path or all the stored values.
216+
217+ Sort the values of a path:
218+
219+ ``` php
220+ $kids = $data->sort('info.kids');
221+
222+ // Sort recursively
223+ $info = $data->sort('info');
224+ ```
225+
226+ Sort all the values
227+
228+ ``` php
229+ $sorted = $data->sort();
230+
231+ // Sort recursively
232+ $sorted = $data->sort();
233+ ```
234+
190235### Magic methods
191236
192237Magic methods can be used to handle single level data (without dot notation). These examples are not using the same data array as examples above.
193238
194- Set value:
239+ Set a value:
195240
196241``` php
197242$data->name = 'John';
198243```
199244
200- Get value:
245+ Get a value:
201246
202247``` php
203248echo $data->name;
204249```
205250
206- Check if value exists:
251+ Check if a value exists:
207252
208253``` php
209254if (isset($data->name)) {
210255 // Do something...
211256}
212257```
213258
214- Delete value:
259+ Delete a value:
215260
216261``` php
217262unset($data->name);
218- ``
263+ ```
0 commit comments