note 127724 added to function.array-merge-recursive
| From: | dev at oove dot blue | Date: | Sat, 15 Oct 2022 00:01:49 +0000 |
| Subject: | note 127724 added to function.array-merge-recursive | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219673@lists.php.net to get a copy of this message | ||
I was looking for a function like this one :
merge one ore more arrays without appending values, just override and extend the array if needed.
Useful for merging configurations or merging json data to be saved in database (my case)
Similar to Object.assign in javascript
public static function array_merge()
{
// Holds all the arrays passed
$params = func_get_args ();
// First array is used as the base, everything else overwrites on it
$return = array_shift ( $params );
if(!is_array($return))
$return = [];
// Merge all arrays on the first array
foreach ( $params as $array ) {
foreach ( $array as $key => $value ) {
if(!isset($return[$key]) || !is_array($value)) {
$return[$key] = $value;
}
else
{
$return[$key] = ArrayTools::array_merge( $return[$key], $value );
}
}
}
return $return;
}
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.159.161 (proxied: 189.217.85.183)
----
Manual Page -- https://php.net/manual/en/function.array-merge-recursive.php
Edit -- https://main.php.net/note/edit/127724
Del: integrated -- https://main.php.net/note/delete/127724/integrated
Del: useless -- https://main.php.net/note/delete/127724/useless
Del: bad code -- https://main.php.net/note/delete/127724/bad+code
Del: spam -- https://main.php.net/note/delete/127724/spam
Del: non-english -- https://main.php.net/note/delete/127724/non-english
Del: in docs -- https://main.php.net/note/delete/127724/in+docs
Del: other reasons-- https://main.php.net/note/delete/127724
Reject -- https://main.php.net/note/reject/127724
Search -- https://main.php.net/manage/user-notes.php