Array-Merge on an associative array in PHP

后端 未结 7 2141
我寻月下人不归
我寻月下人不归 2021-01-04 08:09

How can i do an array_merge on an associative array, like so:

Array 1:

$options = array (
\"1567\" => \"test\",
\"1853\" => \"test1\",
);


        
相关标签:
7条回答
  • 2021-01-04 08:53

    This code could be used for recursive merge:

    function merge($arr1, $arr2){
            $out = array();
            foreach($arr1 as $key => $val1){
                if(isset($arr2[$key])){
                    if(is_array($arr1[$key]) && is_array($arr2[$key])){
                        $out[$key]=  merge($arr1[$key], $arr2[$key]);
                    }else{
                        $out[$key]= array($arr1[$key], $arr2[$key]);
                    }
                    unset($arr2[$key]);
                }else{
                    $out[$key] = $arr1[$key];
                }
            }
            return $out + $arr2;
     }
    
    0 讨论(0)
提交回复
热议问题