2
2
Easy access to multidimensional arrays with dot notation.
3
3
With dot notation, your code is cleaner and handling deeper arrays is super easy.
4
4
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.
6
6
7
7
With Dot you can change this:
8
8
@@ -32,6 +32,17 @@ composer require adbario/php-dot-notation
32
32
33
33
Or just copy the class file Dot.php and handle namespace yourself.
34
34
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
+
35
46
## Usage
36
47
37
48
This array will be used as a reference on this guide:
@@ -55,35 +66,35 @@ $array = [
55
66
];
56
67
```
57
68
58
- ### Create Dot object
69
+ ### Create a Dot object
59
70
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:
61
72
62
73
``` php
63
- $data = new \AdBar \Dot;
74
+ $data = new \Adbar \Dot;
64
75
```
65
76
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:
67
78
68
79
``` php
69
- $data = new \AdBar \Dot($array);
80
+ $data = new \Adbar \Dot($array);
70
81
```
71
82
72
- Set data after creating Dot object:
83
+ Set an array after creating the Dot object:
73
84
74
85
``` php
75
- $data->setData ($array);
86
+ $data->setArray ($array);
76
87
```
77
88
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:
79
90
80
91
``` php
81
- $data->setDataAsRef ($array);
92
+ $data->setReference ($array);
82
93
```
83
94
84
- ### Set value
95
+ ### Set a value
85
96
86
- Set i.e. phone number in 'home' array:
97
+ Set i.e. a phone number in the 'home' array:
87
98
88
99
``` php
89
100
$data->set('info.home.tel', '09-123-456-789');
@@ -101,21 +112,39 @@ $data->set([
101
112
]);
102
113
```
103
114
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.
105
116
106
- ### Get value
117
+ ### Get a value
107
118
108
119
``` php
109
120
echo $data->get('info.home.address');
110
121
111
- // Default value if path doesn't exist
122
+ // Default value if the path doesn't exist
112
123
echo $data->get('info.home.country', 'some default value');
113
124
114
125
// Array style
115
126
echo $data['info.home.address'];
116
127
```
117
128
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
119
148
120
149
``` php
121
150
$data->add('info.kids', 'Amy');
@@ -129,7 +158,7 @@ $data->add('info.kids', [
129
158
]);
130
159
```
131
160
132
- ### Check if value exists
161
+ ### Check if a value exists
133
162
134
163
``` php
135
164
if ($data->has('info.home.address')) {
@@ -142,7 +171,7 @@ if (isset($data['info.home.address'])) {
142
171
}
143
172
```
144
173
145
- ### Delete value
174
+ ### Delete a value
146
175
147
176
``` php
148
177
$data->delete('info.home.address');
@@ -161,18 +190,12 @@ $data->delete([
161
190
162
191
### Clear values
163
192
164
- Delete all values from path:
193
+ Delete all the values from a path:
165
194
166
195
``` php
167
196
$data->clear('info.home');
168
197
```
169
198
170
- If path doesn't exist, create an empty array on it
171
-
172
- ``` php
173
- $data->clear('info.home.rooms', true);
174
- ```
175
-
176
199
Clear multiple paths at once:
177
200
178
201
``` php
@@ -187,32 +210,54 @@ Clear all data:
187
210
$data->clear();
188
211
```
189
212
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
+
190
235
### Magic methods
191
236
192
237
Magic methods can be used to handle single level data (without dot notation). These examples are not using the same data array as examples above.
193
238
194
- Set value:
239
+ Set a value:
195
240
196
241
``` php
197
242
$data->name = 'John';
198
243
```
199
244
200
- Get value:
245
+ Get a value:
201
246
202
247
``` php
203
248
echo $data->name;
204
249
```
205
250
206
- Check if value exists:
251
+ Check if a value exists:
207
252
208
253
``` php
209
254
if (isset($data->name)) {
210
255
// Do something...
211
256
}
212
257
```
213
258
214
- Delete value:
259
+ Delete a value:
215
260
216
261
``` php
217
262
unset($data->name);
218
- ``
263
+ ```
0 commit comments