##1. array_merge
Sample code:$arr1 = array(1, 2, 3, 4, 5); $arr2 = array(1, 2, 6, 7, 8, 9, 10); $result1 = array_merge($arr1, $arr2); $arr3 = array("name" => "itbsl", "age" => 13, "sex" => "Male"); $arr4 = array("name" => "火龙果", "age" => 13, "sex" => "Male"); $result2 = array_merge($arr3, $arr4); echo "<pre class="brush:php;toolbar:false">"; var_dump($result1); var_dump($result2);
PHP Introduction Tutorial"
But for an array of key-value pairs, if there are the same keys, then the second array will overwrite the first An array of values corresponding to the same key.2. Merge through " "
Sample code:$arr1 = array(1, 2, 3, 4, 5); $arr2 = array(1, 2, 6, 7, 8, 9, 10); $result1 = $arr1 + $arr2; $arr3 = array("name" => "itbsl", "age" => 13, "sex" => "Male"); $arr4 = array("name" => "火龙果", "age" => 13, "sex" => "Male", "id" => "411521"); $result2 = $arr3 + $arr4; echo "<pre class="brush:php;toolbar:false">"; var_dump($result1); var_dump($result2);
3. Connect two arrays
array_combine() function will get a new array, which consists of a set of submitted keys and corresponding values. Sample code:$arr1 = array("A","B","C","D"); $arr2 = array("paul","itbsl","Golang","PHP"); $result = array_combine($arr1,$arr2); echo '<pre class="brush:php;toolbar:false">'; var_dump($result);
4. Recursively append arrays
array_merge_recursive() function is the same as array_merge(). It can merge two or more arrays together to form a joint array. . The difference between the two is that the function will handle it differently when a key in an input array already exists in the result array. array_merge() will overwrite the previously existing key/value pairs and replace them with the key/value pairs in the current input array, while array_merge_recursive() will merge the two values together to form a new array with the original keys. as an array name. Sample code:$arr3 = array("name" => "itbsl", "age" => 13, "sex" => "Male"); $arr4 = array("name" => "火龙果", "age" => 13, "sex" => "Male"); $result2 = array_merge_recursive($arr3, $arr4); echo "<pre class="brush:php;toolbar:false">"; var_dump($result2);
The above is the detailed content of What are the ways to merge two arrays in php. For more information, please follow other related articles on the PHP Chinese website!