Req #63743 [Csd]: New function request
Edit report at https://bugs.php.net/bug.php?id=63743&edit=1
ID: 63743
User updated by: Test at test dot com
Reported by: Test at test dot com
Summary: New function request
Status: Closed
Type: Feature/Change Request
Package: Unknown/Other Function
Operating System: All
PHP Version: Irrelevant
Assigned To: arpad
Block user comment: N
Private report: N
New Comment:
Could this please be removed, or at least remove my email address.
Thank you
Previous Comments:
------------------------------------------------------------------------
[2012-12-16 12:18:29] arpad@php.net
This can be done with array_merge_recursive().
------------------------------------------------------------------------
[2012-12-11 17:58:22] Test at test dot com
Description:
------------
Suggestion:
Could you please implement a function to merge associative arrays without overwriting duplicate
keys.
Unless there already is one that I have missed?
I have provided a small example that works to a point that will demonstrate my idea.
Thanks.
Test script:
---------------
function array_merge_assoc($array1, $array2){
foreach($array2 as $key => $value){
if(array_key_exists($key, $array1)){
$result[$key] = array_merge($array1[$key], $array2[$key]);
unset($array2[$key]);
}else{
$result[$key] = $array2[$key];
}
}
return $result;
}
$userConfig = ['libraries' => ['database', 'session',
'security']];
$defaults = ['libraries' => ['benchmark' , 'event',
'error'],
'helpers' => ['html','date','file']];
print_r(array_merge_assoc($userConfig,$defaults));
Expected result:
----------------
Array
(
[libraries] => Array
(
[0] => database
[1] => session
[2] => security
[3] => benchmark
[4] => event
[5] => error
)
[helpers] => Array
(
[0] => html
[1] => date
[2] => file
)
)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=63743&edit=1
Thread (4 messages)