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');
/* Start to develop here. Best regards https://php-download.com/ */
graze / array-merger example snippets
$a = ['key' => 'org value', 'key2' => 'first'];
$b = ['key' => 'new value', 'key2' => null];
array_merge_recursive($a, $b);
// ['key' => ['org value', 'new value'], 'key2' => ['first', null]];
array_replace_recursive($a, $b);
// ['key' => 'new value', 'key2' => null];
RecursiveArrayMerger::lastNonNull($a, $b);
// ['key' => 'new value', 'key2' => 'first']);
$merger = new Graze\ArrayMerger\ArrayMerger();
$merger->merge(
['a' => 'first', 'b' => ['c' => 'cake', 'd' => 'fish']],
['a' => 'second', 'b' => ['d' => 'money']]
);
// ['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']],
);
// ['a' => 'third', 'b' => ['c' => 'cake', 'd' => 'money', 'e' => 'planets]]
$merger = new Graze\ArrayMerger\RecursiveArrayMerger(new LastNonNullValue());
$merger->merge(
['a' => 'first', 'b' => ['c' => 'cake', 'd' => 'fish']],
['a' => 'second', 'b' => ['d' => null]]
);
// ['a' => 'second', 'b' => ['c' => 'cake', 'd' => 'fish']]
$merger = new Graze\ArrayMerger\RecursiveArrayMerger('max');
$merger->merge(
['a' => 1, 'b' => ['c' => 2, 'd' => 3]],
['a' => 4, 'b' => ['d' => 1]]
);
// ['a' => 4, 'b' => ['c' => 2, 'd' => 3]]
// or some strange value choose of your choice
$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]]
);
// ['a' => 1, 'b' => ['c' => 2, 'd' => 0]]
RecursiveArrayMerger::mergeUsing(
new LastValue(),
['a' => 'first', 'b' => ['c' => 'cake', 'd' => 'fish']],
['a' => 'second', 'b' => ['d' => 'money']]
);
// ['a' => 'second', 'b' => ['c' => 'cake', 'd' => 'money']]
RecursiveArrayMerger::lastNonNull(
['a' => 'first', 'b' => ['c' => 'cake', 'd' => 'fish']],
['a' => null, 'b' => ['d' => 'money']]
);
// ['a' => 'first', 'b' => ['c' => 'cake', '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);
// ['a' => 'second', 'b' => ['a','c','d','e']]
$merger = new Graze\ArrayMerger\RecursiveArrayMerger(new LastValue(), RecursiveArrayMerger::FLAG_MERGE_VALUE_ARRAY);
$merger->merge($a,$b);
// ['a' => 'second', 'b' => ['e','c','d']]
$a = ['a' => 'first', 'b' => ['a','c','d']];
$b = ['a' => 'second', 'b' => ['d','e']];
$merger = new Graze\ArrayMerger\RecursiveArrayMerger(new LastValue());
$merger->merge($a,$b);
// ['a' => 'second', 'b' => ['a','c','d','d','e']]
$merger = new Graze\ArrayMerger\RecursiveArrayMerger(new LastValue(), RecursiveArrayMerger::FLAG_UNIQUE_VALUE_ARRAY);
$merger->merge($a,$b);
// ['a' => 'second', 'b' => ['a','c','d','e']]