From: jb7815 at gmail dot com
Operating system: All
PHP version: Irrelevant
Package: Unknown/Other Function
Bug Type: Feature/Change Request
Bug description:New function request
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 bug report at https://bugs.php.net/bug.php?id=63743&edit=1
--
Try a snapshot (PHP 5.4):
https://bugs.php.net/fix.php?id=63743&r=trysnapshot54
Try a snapshot (PHP 5.3):
https://bugs.php.net/fix.php?id=63743&r=trysnapshot53
Try a snapshot (trunk):
https://bugs.php.net/fix.php?id=63743&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=63743&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=63743&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=63743&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=63743&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=63743&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=63743&r=support
Expected behavior: https://bugs.php.net/fix.php?id=63743&r=notwrong
Not enough info:
https://bugs.php.net/fix.php?id=63743&r=notenoughinfo
Submitted twice:
https://bugs.php.net/fix.php?id=63743&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=63743&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=63743&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=63743&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=63743&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=63743&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=63743&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=63743&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=63743&r=mysqlcfg