Edit report at https://bugs.php.net/bug.php?id=63743&edit=1

 ID:                 63743
 Comment by:         ar...@php.net
 Reported by:        jb7815 at gmail dot com
 Summary:            New function request
 Status:             Open
 Type:               Feature/Change Request
 Package:            Unknown/Other Function
 Operating System:   All
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

This can be done with array_merge_recursive().


Previous Comments:
------------------------------------------------------------------------
[2012-12-11 17:58:22] jb7815 at gmail 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

Reply via email to