https://github.com/andydune/arraywalker
Better implementation foreach operator with nested array compability.
https://github.com/andydune/arraywalker
array array-manipulations php php7
Last synced: 6 months ago
JSON representation
Better implementation foreach operator with nested array compability.
- Host: GitHub
- URL: https://github.com/andydune/arraywalker
- Owner: AndyDune
- License: mit
- Created: 2018-05-18T05:37:13.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2020-05-03T18:25:33.000Z (about 6 years ago)
- Last Synced: 2025-03-25T05:43:31.995Z (over 1 year ago)
- Topics: array, array-manipulations, php, php7
- Language: PHP
- Size: 7.81 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ArrayWalker
[](https://travis-ci.org/AndyDune/ArrayWalker)
[](LICENSE)
[](https://packagist.org/packages/andydune/array-walker)
[](https://packagist.org/packages/andydune/array-walker)
Better implementation foreach operator with nested array compability.
Installation
------------
Installation using composer:
```
composer require andydune/array-walker
```
Or if composer was not installed globally:
```
php composer.phar require andydune/array-walker
```
Or edit your `composer.json`:
```
"require" : {
"andydune/array-walker": "^1"
}
```
And execute command:
```
php composer.phar update
```
Example
------------
```php
use AndyDune\ArrayWalker\ArrayWalker;
use AndyDune\ArrayWalker\ItemContainer;
// Source array
$array = [
'one' => 1,
'two' => 2,
'three' => 3,
];
$arrayWalker = new ArrayWalker($array);
// Change values
$arrayWalker->addFunction(function (ItemContainer $item) {
$item->setValue($item->getValue() + 10);
});
$result = $arrayWalker->apply();
$result = [
'one' => 11,
'two' => 12,
'three' => 13,
];
$arrayWalker = new ArrayWalker($array);
// Change keys
$arrayWalker->addFunction(function (ItemContainer $item) {
$item->setKey(strtoupper($item->getKey()));
});
$result = $arrayWalker->apply();
$result = [
'ONE' => 1,
'TWO' => 2,
'THREE' => 3,
];
$arrayWalker = new ArrayWalker($array);
// Delete value
$arrayWalker = new ArrayWalker($array);
$arrayWalker->addFunction(function (ItemContainer $item) {
$item->setValue($item->getValue() + 10);
if ($item->getKey() == 'one') {
$item->delete();
}
});
$result = $arrayWalker->apply();
$result = [
'two' => 12,
'three' => 13,
];
```