https://github.com/czproject/arrays
Array tools library.
https://github.com/czproject/arrays
arrays php
Last synced: over 1 year ago
JSON representation
Array tools library.
- Host: GitHub
- URL: https://github.com/czproject/arrays
- Owner: czproject
- License: other
- Created: 2016-08-19T18:08:09.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2023-03-13T14:25:48.000Z (over 3 years ago)
- Last Synced: 2025-02-12T06:53:54.048Z (over 1 year ago)
- Topics: arrays, php
- Language: PHP
- Size: 14.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license.md
Awesome Lists containing this project
README
# CzProject\Arrays
[](https://github.com/czproject/arrays/actions)
[](https://packagist.org/packages/czproject/arrays)
[](https://github.com/czproject/arrays/releases)
[](https://github.com/czproject/arrays/blob/master/license.md)
Array tools library.
## Installation
[Download a latest package](https://github.com/czproject/arrays/releases) or use [Composer](http://getcomposer.org/):
```
composer require czproject/arrays
```
`CzProject\Arrays` requires PHP 5.6.0 or later.
## Usage
``` php
use CzProject\Arrays;
```
### `flatten()`
``` php
$data = Arrays::flatten(array(
'value 1',
'values' => array(
'value 2-1',
'value 2-2',
'value 2-3',
),
'value 3',
));
/* Returns:
[
'value 1',
'value 2-1',
'value 2-2',
'value 2-3',
'value 3',
]
*/
```
### `fetchPairs()`
``` php
$rows = array(
array(
'id' => 1,
'name' => 'Row #1',
),
array(
'id' => 2,
'name' => 'Row #2',
),
array(
'id' => 3,
'name' => 'Row #3',
),
);
$data = Arrays::fetchPairs($rows, 'id', 'name');
/* Returns:
[
1 => 'Row #1',
2 => 'Row #2',
3 => 'Row #3',
]
*/
```
### `merge()`
``` php
$defaultConfig = array(
'parameters' => array(
'database' => array(
'host' => 'localhost',
'database' => 'lorem_ipsum',
'driver' => 'mysql',
),
),
'messages' => array(
'success' => 'Success!',
'error' => 'Error!',
),
);
$config = array(
'parameters' => array(
'database' => array(
'user' => 'user123',
'password' => 'password123',
),
),
'messages' => array(
'error' => 'Fatal Error!',
),
);
$data = Arrays::merge($config, $defaultConfig);
/* Returns:
[
parameters => [
database => [
host => 'localhost',
database => 'lorem_ipsum',
driver => 'mysql',
user => 'user123',
password => 'password123',
]
],
messages => [
success => 'Success!',
error => 'Fatal Error!',
]
]
*/
```
### `pushFrom()`
``` php
$a = ['A1', 'A2', 'A3', 'A4'];
$b = ['B1', 'B2'];
$result = [];
for ($i = 0; $i < 4; $i++) {
Arrays::pushFrom($result, $a);
Arrays::pushFrom($result, $b);
}
/* Returns:
[
'A1',
'B1',
'A2',
'B2',
'A3',
'A4',
]
*/
```
------------------------------
License: [New BSD License](license.md)
Author: Jan Pecha, https://www.janpecha.cz/