1
1
# Arrayzy
2
2
3
- A native PHP arrays easy manipulation library in OOP way .
3
+ The wrapper for all PHP built-in array functions and easy, object-oriented array manipulation library. In short: Arrays on steroids .
4
4
5
5
[ ![ SensioLabsInsight] ( https://insight.sensiolabs.com/projects/e0235f5d-a89b-4add-b3c6-45813d2bf9eb/mini.png )] ( https://insight.sensiolabs.com/projects/e0235f5d-a89b-4add-b3c6-45813d2bf9eb )
6
6
[ ![ Build Status] ( https://travis-ci.org/bocharsky-bw/Arrayzy.svg?branch=master )] ( https://travis-ci.org/bocharsky-bw/Arrayzy )
7
7
[ ![ Scrutinizer Code Quality] ( https://scrutinizer-ci.com/g/bocharsky-bw/Arrayzy/badges/quality-score.png?b=master )] ( https://scrutinizer-ci.com/g/bocharsky-bw/Arrayzy/?branch=master )
8
8
[ ![ Code Coverage] ( https://scrutinizer-ci.com/g/bocharsky-bw/Arrayzy/badges/coverage.png?b=master )] ( https://scrutinizer-ci.com/g/bocharsky-bw/Arrayzy/?branch=master )
9
9
10
- There are two available classes with a different behaviour :
10
+ There are two classes available with different behavior :
11
11
12
12
* [ Arrayzy\MutableArray] ( #mutablearray )
13
13
* [ Arrayzy\ImmutableArray] ( #immutablearray )
14
14
15
15
## MutableArray
16
16
17
- Each functional method operate on the same array and return the same instance
18
- (** DO NOT** create a new instance) except only few methods starts with ` create ` prefix.
19
- This way a bit improve performance and provide more convenience usage in OOP way.
17
+ Each method operates on the same array and returns the array itself
18
+ (it ** DOES NOT** create a new instance), except methods starting with the
19
+ ` create ` prefix. This way has slightly better performance and is more
20
+ convenient to use in an OOP way.
20
21
21
- > ** NOTE:** Check the [ CreateClone] ( #createclone ) section if you want to operate on new instance NOT on same instance .
22
+ > ** NOTE:** Check the [ CreateClone] ( #createclone ) section if you want to operate on a new instance to preserve the current one .
22
23
23
24
## ImmutableArray
24
25
25
- Each functional method operate on the same array but not modified it.
26
- Instead of this it return a new object (create a new instance).
27
- This way a bit disimprove performance but give you the behaviour
28
- seems like most of built-in PHP functions that returns a new array (** DO NOT** modified input one).
26
+ Each method creates a new array, leaving the original array unchanged.
27
+ This way has slightly worse performance but give you a behavior similar
28
+ to most of the built-in PHP functions that return a new array.
29
29
30
- > ** NOTE:** If you don't need the first instance you operates on, you can override it manually:
30
+ > ** NOTE:** If you don't need the first array you operate on, you can override it manually:
31
31
32
32
``` php
33
33
$a = ImmutableArray::create(['a', 'b', 'c']);
@@ -122,39 +122,41 @@ $ composer require bocharsky-bw/arrayzy:dev-master
122
122
```
123
123
124
124
If you don't use ` Composer ` - register this package in your autoloader manually
125
- or download library manually and require the necessary files directly in your scripts:
125
+ or download the library manually and ` require ` the necessary files directly in your scripts:
126
126
127
127
``` php
128
128
require_once __DIR__ . '/path/to/library/src/MutableArray.php';
129
129
```
130
130
131
- Don't forget namespace . Use [ namespace arbitrary alias ] [ 2 ] for simplicity if you want:
131
+ Don't forget namespaces . Use [ namespace aliases ] [ 2 ] for simplicity if you want:
132
132
133
133
``` php
134
- use Arrayzy\MutableArray as MuArr; //
135
- use Arrayzy\ImmutableArray as ImArr; // MuArr and ImArr is the arbitrary aliases
134
+ use Arrayzy\MutableArray as MuArr;
135
+ use Arrayzy\ImmutableArray as ImArr;
136
136
```
137
137
138
138
## Creation
139
139
140
- Create a new empty object with the ` new ` statement using declared arbitrary namespace aliases above:
140
+ Create a new empty array with the ` new ` statement.
141
141
142
142
``` php
143
- $a = new MuArr ; // Create new instance of MutableArray by namespace alias
143
+ $a = new MutableArray ; // Create new instance of MutableArray
144
144
// or
145
- $a = new ImArr; // Create new instance of ImmutableArray by namespace alias
145
+ $a = new ImmutabelArray; // Create new instance of ImmutableArray
146
+ // or
147
+ $a = new MuArr; // using namespace aliases
146
148
```
147
149
148
- or with default values, passed an array to the constructor:
150
+ or with default values, passed to the constructor in an array :
149
151
150
152
``` php
151
- $a = new MuArr ([1, 2, 3]);
153
+ $a = new MutableArray ([1, 2, 3]);
152
154
// or
153
- $a = new ImArr ([1, 2, 3]);
155
+ $a = new ImmutabelArray ([1, 2, 3]);
154
156
```
155
157
156
- Also, a new object could be created with one of few public ` static ` methods
157
- that starts with ` create ` prefix and provide additional useful functionality :
158
+ Also, new objects can be created with one of the public static methods
159
+ prefixed with ' create' :
158
160
159
161
* [ create] ( #create )
160
162
* [ createFromJson] ( #createfromjson )
@@ -164,7 +166,7 @@ that starts with `create` prefix and provide additional useful functionality:
164
166
165
167
## Usage
166
168
167
- You can get access to the instance values like with simple PHP ` array ` behaviour :
169
+ You can get access to the values like with the familiar PHP array syntax :
168
170
169
171
``` php
170
172
use Arrayzy\MutableArray as A;
@@ -178,16 +180,18 @@ $a[3] = 'd'; // or use $a->offsetSet(3, 'd') method
178
180
$a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd']
179
181
180
182
print $a[1]; // 'b'
181
- // or use corresponding method
183
+ // or use the corresponding method
182
184
print $a->offsetGet(1); // 'b'
183
185
```
184
186
185
- > ** NOTE:** Further will be used the ` A ` namespace alias to shorten usage examples, but keep in mind
186
- that the usage are suitable for both ` MutableArray ` and ` ImmutableArray ` in this case.
187
+ * NOTE: The following methods and principles apply to ` ImmutableArray ` and
188
+ ` MutableArray ` alike. In the examples provided they are are interchangeable and
189
+ aliased with ` A ` .*
187
190
188
191
### Chaining
189
192
190
- Use chaining methods for fast usage:
193
+
194
+ Methods may be chained for ease of use:
191
195
192
196
``` php
193
197
$a = A::create(['a', 'b', 'c']);
197
201
->offsetSet(3, 'd')
198
202
->offsetSet(null, 'e')
199
203
->shuffle()
200
- ->reindex() // or any other method that return the current instance
204
+ ->reindex() // or any other method that returns the current instance
201
205
;
202
206
203
207
$a->toArray(); // [0 => 'c', 1 => 'a', 2 => 'e', 3 => 'd', 4 => 'b']
204
208
```
205
209
206
210
### Converting
207
211
208
- Easily converting instance array elements to a simple PHP ` array ` , ` string ` ,
209
- ` readable string ` or ` JSON ` format:
212
+ Easily convert instance array elements to a simple PHP ` array ` , ` string ` ,
213
+ readable ` string ` or JSON format:
210
214
211
215
* [ toArray] ( #toarray )
212
216
* [ toJson] ( #tojson )
@@ -282,14 +286,16 @@ $a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c']
282
286
283
287
### createClone
284
288
285
- Keep in mind, that in PHP variables contains only reference to the object, ** NOT** the same object:
289
+ Creates a shallow copy of the array.
290
+
291
+ Keep in mind, that in PHP variables contain only references to the object, ** NOT** the object itself:
286
292
287
293
``` php
288
294
$a = A::create(['a', 'b', 'c']);
289
- $b = $a; // $a and $b are different variables referenced to the same object ($a === $b)
295
+ $b = $a; // $a and $b are different variables referencing the same object ($a === $b)
290
296
```
291
297
292
- So if you ** DO NOT** want to modify current instance , you need to clone it manually first:
298
+ So if you ** DO NOT** want to modify the current array , you need to clone it manually first:
293
299
294
300
``` php
295
301
$a = A::create(['a', 'b', 'c']);
@@ -300,7 +306,7 @@ $b = $a->createClone(); // $a !== $b
300
306
301
307
### createFromJson
302
308
303
- Creates an instance array from a valid ` JSON ` string:
309
+ Creates an array by parsing a JSON string:
304
310
305
311
``` php
306
312
$a = A::createFromJson('{"a": 1, "b": 2, "c": 3}');
@@ -319,7 +325,7 @@ $b->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c']
319
325
320
326
### createFromString
321
327
322
- Creates an instance array from a simple PHP ` string ` with specified separator:
328
+ Creates an array from a simple PHP ` string ` with specified separator:
323
329
324
330
``` php
325
331
$a = A::createFromString('a;b;c', ';');
@@ -328,7 +334,7 @@ $a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c']
328
334
329
335
### createWithRange
330
336
331
- Creates an instance array with specified range:
337
+ Creates an array of a specified range:
332
338
333
339
``` php
334
340
$a = A::createWithRange(2, 6, 2);
@@ -337,6 +343,8 @@ $a->toArray(); // [0 => 2, 1 => 4, 2 => 6]
337
343
338
344
### current
339
345
346
+ Position of the iterator.
347
+
340
348
``` php
341
349
$a = A::create(['a', 'b', 'c']);
342
350
$a->current(); // 'a'
@@ -404,7 +412,7 @@ $a->export(); // array ( 0 => 'a', 1 => 'b', 2 => 'c', )
404
412
``` php
405
413
$a = A::create(['a', 'z', 'b', 'z']);
406
414
$a->filter(function($value) {
407
- return 'z' !== $value; // exclude 'z' value from array
415
+ return 'z' !== $value; // exclude 'z' value from array
408
416
});
409
417
$a->toArray(); // [0 => 'a', 2 => 'b']
410
418
```
@@ -483,7 +491,7 @@ $a->getValues(); // [0 => 'a', 1 => 'b', 2 => 'c']
483
491
484
492
``` php
485
493
$a = A::create(['a', 'b', 'c']);
486
- $a->indexOf('b'); // 1
494
+ $a->indexOf('b'); // 1
487
495
```
488
496
489
497
### isEmpty
@@ -571,10 +579,10 @@ $a->offsetGet(1); // 'b' (or use $a[1])
571
579
572
580
``` php
573
581
$a = A::create(['a', 'b', 'd']);
574
- // add new value
582
+ // add a new value
575
583
$a->offsetSet(null, 'd'); // or use $a[] = 'd';
576
584
$a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'd', 3=> 'd']
577
- // replace existing value by key
585
+ // replace an existing value by key
578
586
$a->offsetSet(2, 'c'); // or use $a[2] = 'c';
579
587
$a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c', 3=> 'd']
580
588
```
@@ -620,7 +628,7 @@ $a->push('c', 'd');
620
628
$a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd']
621
629
```
622
630
623
- > Method ` push() ` allow multiple arguments.
631
+ > The ` push() ` method allows multiple arguments.
624
632
625
633
### reduce
626
634
@@ -705,7 +713,7 @@ $a->toArray(); // [0 => 'd', 1 => 'b', 2 => 'c', 3 => 'a']
705
713
706
714
### toArray
707
715
708
- Converts instance array to a simple PHP ` array ` :
716
+ Convert the array to a simple PHP ` array ` :
709
717
710
718
``` php
711
719
$a = A::create(['a', 'b', 'c']);
@@ -714,7 +722,7 @@ $a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c']
714
722
715
723
### toJson
716
724
717
- Converts instance array to a ` JSON ` string:
725
+ Creates a JSON string from the array :
718
726
719
727
``` php
720
728
$a = A::create(['a' => 1, 'b' => 2, 'c' => 3]);
@@ -763,17 +771,17 @@ $a->toArray(); // [0 => 'y', 1 => 'z', 2 => 'a', 3 => 'b']
763
771
$a = A::create(['a', 'b', 'c']);
764
772
$a->walk(function(& $value, $key) {
765
773
$key++; // the $key variable passed by value, (original value will not modified)
766
- $value = $value . $key; // the $value variable passed by reference (modify original value)
774
+ $value = $value . $key; // the $value variable passed by reference (modifies original value)
767
775
});
768
776
$a->toArray(); // [0 => 'a1', 1 => 'b2', 2 => 'c3']
769
777
```
770
778
771
779
## Links
772
780
773
- Feel free to create an [ Issue] [ 3 ] or [ Pull Request] [ 4 ] if you find a bug
774
- or just want to propose improvement suggestion.
781
+ Feel free to create an [ Issue] [ 3 ] or [ Pull Request] [ 4 ] if you find a bug
782
+ or just want to propose an improvement suggestion.
775
783
776
- Look at the [ Stringy] [ 5 ] if you are looking for a PHP ** string** manipulation library in OOP way.
784
+ Look at the [ Stringy] [ 5 ] if you are looking for a PHP ** string** manipulation library in an OOP way.
777
785
778
786
[ Move UP] ( #arrayzy )
779
787
0 commit comments