|
| 1 | +# array-helper |
| 2 | + |
| 3 | +[](https://packagist.org/packages/awssat/array-helper) |
| 4 | +[](https://travis-ci.org/awssat/array-helper) |
| 5 | + |
| 6 | + |
| 7 | +⚡️ A flexible, simple & yet powerful array manipulation helper for PHP. It gives you the magic of method chaining and it's easier and shorter to be included in views. It Supports most of [PHP built-in array functions](http://php.net/manual/en/book.array.php) |
| 8 | + |
| 9 | + |
| 10 | +```php |
| 11 | +arr([' ', 'hi ', null, ' welcome'])->map('trim')->filter()->get() |
| 12 | +>> ['hi', 'welcome'] |
| 13 | +``` |
| 14 | +## Features |
| 15 | +- Support All PHP array functions. |
| 16 | +- Short methods names, no need to write "array_" like array_map can be only map(..) |
| 17 | +- Array items can be retrireve or updated as properties or keys [->key or [key] ] |
| 18 | +- Support powefull conditional methods. if, else, if{AnyMethod}, endif. |
| 19 | +- Support camelCase and snake-case methods name. |
| 20 | +- Useful new methods like equal, exists .. etc |
| 21 | + |
| 22 | +## Install/Use |
| 23 | +You can install the package via composer locally in your project folder: |
| 24 | + |
| 25 | +```bash |
| 26 | +$ composer require awssat/array-helper |
| 27 | +``` |
| 28 | + |
| 29 | +After installing it, just start using the helper `arr([...])` or `ArrayHelper::make([...])` |
| 30 | + |
| 31 | +## Examples |
| 32 | + |
| 33 | + |
| 34 | +Use any array function, no need for "array_", if you like it you can use it as in array_filter or arrayFilter .. all will work. |
| 35 | +```php |
| 36 | +$x = arr(['', 'item', null, 'item2'])->filter()->get() |
| 37 | +>> ['item', 'item2'] |
| 38 | +``` |
| 39 | + |
| 40 | +You can use conditions, if(function() {...}), if{AnyMethod}, else(), endif() |
| 41 | + |
| 42 | +```php |
| 43 | +$x = arr(['item', 'item2', null]) |
| 44 | + ->ifContains(null) |
| 45 | + ->filter() |
| 46 | + ->endif() |
| 47 | + ->get() |
| 48 | +``` |
| 49 | +you may also use useful method with `if` like `ifEmpty`, `ifKeyExists` or `ifEqual` etc. |
| 50 | + |
| 51 | + |
| 52 | +get() will return all items, while get(index) return a item in the array. all() is alias for get(all, true) which will ignore conditions and force return of items anyway. |
| 53 | + |
| 54 | +The example above can be shortened using all() like this: |
| 55 | +```php |
| 56 | +$x = arr(['item', 'item2', null]) |
| 57 | + ->ifContains(null) |
| 58 | + ->filter() |
| 59 | + ->all() |
| 60 | +``` |
| 61 | + |
| 62 | +You can use do(callback) to run a callback on the array. |
| 63 | + |
| 64 | +```php |
| 65 | +$x = arr(['item ', 'item2']) |
| 66 | + ->do(function() { |
| 67 | + return $this->map('trim'); |
| 68 | + }) |
| 69 | + ->all() |
| 70 | +``` |
| 71 | + |
| 72 | + |
| 73 | +PHP built-in array_map, array_walk, array_filter, and array_reduce are the best! |
| 74 | + |
| 75 | + array_map to loop through all items and change them. |
| 76 | +```php |
| 77 | +$array->map(function($item) { |
| 78 | + return 'Hello: '. strip_tags($item) . ' !'; |
| 79 | + }); |
| 80 | +``` |
| 81 | +or for simple functions use, `$array->map('trim')` |
| 82 | + |
| 83 | +array_walk can be used for looping without changing items |
| 84 | +```php |
| 85 | +$array->walk(function($item, $key) { |
| 86 | + print "$key: $item<br />\n"; |
| 87 | + }); |
| 88 | +``` |
| 89 | + |
| 90 | +or if you want to change the values |
| 91 | +```php |
| 92 | +$array->walk(function(&$item, $key) { |
| 93 | + $item = $item * 2; |
| 94 | + }); |
| 95 | +``` |
| 96 | + |
| 97 | +array_filter is great for filtering an array |
| 98 | +```php |
| 99 | +$array->filter(function($item) { |
| 100 | + return $item > 5; |
| 101 | + }); |
| 102 | +``` |
| 103 | +or just `$array->filter()` to remove any value equal to FALSE. |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +## Tests |
| 109 | +Simply use: |
| 110 | +```bash |
| 111 | +$ composer test |
| 112 | +``` |
| 113 | +## Credits |
| 114 | +- [Abdulrahman M.](https://github.com/abdumu) |
| 115 | +- [All Contributors](../../contributors) |
| 116 | + |
| 117 | +## License |
| 118 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments