PHP code example of andydune / array-walker

1. Go to this page and download the library: Download andydune/array-walker library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

andydune / array-walker example snippets


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,
];

php composer.phar 

php composer.phar update