note 125329 added to function.array-merge-recursive
| From: | andrewatindigo-spheredotcodotuk@php.net | Date: | Fri, 04 Sep 2020 11:27:14 +0000 |
| Subject: | note 125329 added to function.array-merge-recursive | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-215073@lists.php.net to get a copy of this message | ||
An alternative solution where this function does not produce the desired output: Pass a custom
recursive function to array_reduce():
For example (Using PHP 7 capabilities to create recursive anonymous function):
<?php
function array_merge_recursive(...$arrays): ?array {
return array_reduce($arrays, new class {
public function __invoke($carry, $item) {
if (is_array($carry) && is_array($item) {
return $this($carry, $item);
}
return $item ?: $carry;
}
}
}
?>
In this case truthy values will overwrite the value in the previous array.
Of course this is presented as a simple example only, and is not a general solution.
----
Server IP: 45.112.84.4
Probable Submitter: 103.51.167.36 (proxied: 217.155.37.251)
----
Manual Page -- http://php.net/manual/en/function.array-merge-recursive.php
Edit -- https://master.php.net/note/edit/125329
Del: integrated -- https://master.php.net/note/delete/125329/integrated
Del: useless -- https://master.php.net/note/delete/125329/useless
Del: bad code -- https://master.php.net/note/delete/125329/bad+code
Del: spam -- https://master.php.net/note/delete/125329/spam
Del: non-english -- https://master.php.net/note/delete/125329/non-english
Del: in docs -- https://master.php.net/note/delete/125329/in+docs
Del: other reasons-- https://master.php.net/note/delete/125329
Reject -- https://master.php.net/note/reject/125329
Search -- https://master.php.net/manage/user-notes.php