Concatenating two arrays while keeping original keys in PHP

Method 1:using the + operator. 
Example: $array1 = array ( 1 = > 'Geeks' , 2 = > 'For' , 3 = > 'Geeks' ); $array2 = array ( 4 = > 'A' , 5 = > 'Computer' , 6 = > ' Science' , 7 = > 'Portal' , 8 = > 'For' , 9 = > 'Geeks' ); $merged_array = $array1 + $array2 ; var_dump ( $merged_array ); ?> Exit:
array (9 ) {[1] = > string (5) "Geeks" [2] = > string (3) "For" [3] = > string (5) "Geeks" [4] = > string (1) "A" [5] = > string (8) "Computer" [6] = > string (7) "Science" [7] = > string (6) "Portal" [8] = > string (3) "For" [9] = > string (5) "Geeks"} 
Method 2:Using the built-in function array_replace()
Example: $array1 = array ( 1 = > 'Geeks' , 2 = > 'For' , 3 = > 'Geeks' ); $array2 = array ( 4 = > 'A' , 5 = > 'Computer' , 6 = > ' Science' , 7 = > 'Portal' , 8 = > 'For' , 9 = > 'Geeks' ); $merged_array = array_replace ( $array1 , $array2 ); var_dump ( $merged_array ); ?> Exit:
array (9 ) {[1] = > string (5) "Geeks" [2] = > string (3) "For" [3] = > string (5) "Geeks" [4] = > string (1) "A" [5] = > string (8) "Computer" [6] = > string (7) "Science" [7] = > string (6) "Portal" [8] = > string (3) "For" [9] = > string (5) "Geeks"} 
Link: http: //php.net/manual/en/function.array-replace.php



Get Solution for free from DataCamp guru