https://github.com/bluzphp/collection
Collection Package
https://github.com/bluzphp/collection
bluz-package php php-library
Last synced: about 2 months ago
JSON representation
Collection Package
- Host: GitHub
- URL: https://github.com/bluzphp/collection
- Owner: bluzphp
- License: mit
- Created: 2019-05-30T10:24:18.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-11-08T13:28:44.000Z (over 2 years ago)
- Last Synced: 2024-04-26T23:03:25.629Z (almost 2 years ago)
- Topics: bluz-package, php, php-library
- Language: PHP
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Collection Component
## Achievements
[](https://php.net/)
[](https://packagist.org/packages/bluzphp/collection)
[](https://scrutinizer-ci.com/g/bluzphp/collection/build-status/master)
[](https://scrutinizer-ci.com/g/bluzphp/collection/)
[](https://packagist.org/packages/bluzphp/collection)
[](https://packagist.org/packages/bluzphp/collection)
## Usage
Example of an array:
```php
$arr = [
'a',
'b' => 'boo',
'c' => [
'c1',
'c2',
'c3'
],
'd' => [
'd1' => ['d1.1', 'd1.2'],
'd2' => ['d2.1', 'd2.2'],
'd3' => ['d3.1', 'd3.2'],
],
'e' => null,
'f' => false
];
```
### Check the element by key(s)
Usage:
```php
Collection::has(array $array, ...$keys);
array_has($array, ...$keys);
```
Examples:
```php
array_has($array, 0); // true
array_has($array, 'b'); // true
array_has($array, 'c', 0); // true
array_has($array, 'd', 'd1'); // true
array_has($array, 'e'); // true
array_has($array, 'f'); // true
array_has($array, 'g'); // false
```
Compare to `isset()`:
```php
isset($array['e']); // false
```
### Get the element by key(s)
Usage:
```php
Collection::get(array $array, ...$keys);
array_get(array $array, ...$keys);
```
Examples:
```php
array_get($array, 0); // 'a'
array_get($array, 'b'); // 'boo'
array_get($array, 'c', 0); // 'c1'
array_get($array, 'd', 'd1'); // ['d1.1', 'd1.2']
array_get($array, 'e'); // null
array_get($array, 'e', 'e'); // null
```
### Add the element to the array by key(s)
Usage:
```php
Collection::add(array &$array, $key, ...$values);
array_add(array &$array, $key, ...$values);
```
Examples:
```php
array_add($array, 'c', 'c3'); // $array['c'][] = 'c3'
array_add($array, 'd', 'd1', 'd1.3'); // $array['d']['d1'][] = 'd1.3'
array_add($array, 'g', 'g1', 'g1.1'); // $array['g']['g1'][] = 'g1.1'
// but
array_add($array, 'b', 'b1'); // InvalidArgumentException - $array['b'] is not an array
```
### Set the element of the array by key(s)
This method is similar to native way.
Usage:
```php
Collection::set(array &$array, $key, ...$values);
array_set(array &$array, $key, ...$values);
```
Examples:
```php
array_set($array, 'g', 'game over'); // $array['g'] = 'game over';
array_set($array, 'h', 'high way', 'e95'); // $array['h']['high way'] = 'e95';
```