Combine two arrays

前端 未结 9 623
名媛妹妹
名媛妹妹 2020-11-22 15:47

I have two arrays like this:

array( 
\'11\' => \'11\',
\'22\' => \'22\',
\'33\' => \'33\',
\'44\' => \'44\'
);

array( 
\'44\' => \'44\',
\'55         


        
9条回答
  •  不知归路
    2020-11-22 15:57

    To do this, you can loop through one and append to the other:

     '11',
    '22' => '22',
    '33' => '33',
    '44' => '44'
    );
    
    $test2 = array( 
    '44' => '44',
    '55' => '55',
    '66' => '66',
    '77' => '77'
    );
    
    
    function combineWithKeys($array1, $array2)
    {
        foreach($array1 as $key=>$value) $array2[$key] = $value;
        asort($array2);
        return $array2;
    } 
    
    print_r(combineWithKeys($test1, $test2));
    
    ?>
    

    UPDATE: KingCrunch came up with the best solution: print_r($array1+$array2);

提交回复
热议问题