PHP code example of graze / array-merger
1. Go to this page and download the library: Download graze/array-merger 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' );
graze / array-merger example snippets
$a = ['key' => 'org value' , 'key2' => 'first' ];
$b = ['key' => 'new value' , 'key2' => null ];
array_merge_recursive($a, $b);
array_replace_recursive($a, $b);
RecursiveArrayMerger::lastNonNull($a, $b);
$merger = new Graze\ArrayMerger\ArrayMerger();
$merger->merge(
['a' => 'first' , 'b' => ['c' => 'cake' , 'd' => 'fish' ]],
['a' => 'second' , 'b' => ['d' => 'money' ]]
);
$merger = new Graze\ArrayMerger\RecursiveArrayMerger();
$merger->merge(
['a' => 'first' , 'b' => ['c' => 'cake' , 'd' => 'fish' ]],
['a' => 'second' , 'b' => ['d' => 'money' ]],
['a' => 'third' , 'b' => ['e' => 'planets' ]],
);
$merger = new Graze\ArrayMerger\RecursiveArrayMerger(new LastNonNullValue());
$merger->merge(
['a' => 'first' , 'b' => ['c' => 'cake' , 'd' => 'fish' ]],
['a' => 'second' , 'b' => ['d' => null ]]
);
$merger = new Graze\ArrayMerger\RecursiveArrayMerger('max' );
$merger->merge(
['a' => 1 , 'b' => ['c' => 2 , 'd' => 3 ]],
['a' => 4 , 'b' => ['d' => 1 ]]
);
$merger = new Graze\ArrayMerger\RecursiveArrayMerger(
function ($a, $b) {
return $a % $b == 0 ? $a : $b;
}
);
$merger->merge(
['a' => 1 , 'b' => ['c' => 2 , 'd' => 3 ]],
['a' => 4 , 'b' => ['d' => 1 ]]
);
RecursiveArrayMerger::mergeUsing(
new LastValue(),
['a' => 'first' , 'b' => ['c' => 'cake' , 'd' => 'fish' ]],
['a' => 'second' , 'b' => ['d' => 'money' ]]
);
RecursiveArrayMerger::lastNonNull(
['a' => 'first' , 'b' => ['c' => 'cake' , 'd' => 'fish' ]],
['a' => null , 'b' => ['d' => 'money' ]]
);
$a = ['a' => 'first' , 'b' => ['a' ,'c' ,'d' ]];
$b = ['a' => 'second' , 'b' => ['e' ]];
$merger = new Graze\ArrayMerger\RecursiveArrayMerger(new LastValue());
$merger->merge($a,$b);
$merger = new Graze\ArrayMerger\RecursiveArrayMerger(new LastValue(), RecursiveArrayMerger::FLAG_MERGE_VALUE_ARRAY);
$merger->merge($a,$b);
$a = ['a' => 'first' , 'b' => ['a' ,'c' ,'d' ]];
$b = ['a' => 'second' , 'b' => ['d' ,'e' ]];
$merger = new Graze\ArrayMerger\RecursiveArrayMerger(new LastValue());
$merger->merge($a,$b);
$merger = new Graze\ArrayMerger\RecursiveArrayMerger(new LastValue(), RecursiveArrayMerger::FLAG_UNIQUE_VALUE_ARRAY);
$merger->merge($a,$b);