Home > Article > Backend Development > PHP function application array merging
In PHP programming, array (array) is a very commonly used data type. PHP provides many functions for operating arrays, including array merging (marge) functions. Array merging means combining elements from two or more arrays into one array. In this article, we will introduce the array merging function in PHP and its applications.
mixed array_merge ( array $array1 [, array $... ] )
Among them, the parameter array1 represents the first array to be merged, and the following parameter $... represents other arrays to be merged. For example:
$arr1 = array('apple', 'banana'); $arr2 = array('cat', 'dog', 'elephant'); $result = array_merge($arr1, $arr2); print_r($result);
The output result of the above code is:
Array ( [0] => apple [1] => banana [2] => cat [3] => dog [4] => elephant )
As can be seen from the result, the function array_merge() merges the elements in the two arrays $arr1 and $arr2 into a new in the array $result and arranged in the original order.
It should be noted that if the same key name (key) exists in the merged array, the later value will overwrite the previous value. For example:
$arr1 = array('a' => 'apple', 'b' => 'banana'); $arr2 = array('a' => 'orange', 'c' => 'cherry'); $result = array_merge($arr1, $arr2); print_r($result);
The output result of the above code is:
Array ( [a] => orange [b] => banana [c] => cherry )
As can be seen from the result, the key name 'a' in the array $arr2 covers the key name 'a' in the array $arr1 ', and the values of key names 'b' and 'c' are not merged.
mixed array_merge_recursive ( array $array1 [, array $... ] )
For example:
$arr1 = array('a' => array('apple'), 'b' => array('banana')); $arr2 = array('a' => array('orange'), 'c' => array('cherry')); $result = array_merge_recursive($arr1, $arr2); print_r($result);
The output result of the above code is:
Array ( [a] => Array ( [0] => apple [1] => orange ) [b] => Array ( [0] => banana ) [c] => Array ( [0] => cherry ) )
As can be seen from the result, the key name 'a' corresponds to The value is an array. When merging, the function array_merge_recursive() will recursively merge the values in the array.
It should be noted that if the same key name exists in the merged array, their values will be merged into one array. For example:
$arr1 = array('a' => array('apple'), 'b' => array('banana')); $arr2 = array('a' => array('orange'), 'b' => array('cherry')); $result = array_merge_recursive($arr1, $arr2); print_r($result);
The output result of the above code is:
Array ( [a] => Array ( [0] => apple [1] => orange ) [b] => Array ( [0] => banana [1] => cherry ) )
As can be seen from the result, the value corresponding to the key name 'a' is an array. When merging, the function array_merge_recursive() will Recursively combine values in an array. For key name 'b', since the same key name exists, their values will be merged into an array.
mixed array_replace ( array $array1, array $array2 [, array $... ] )
Among them, the parameter $array1 represents the array to be replaced, the parameter $array2 represents the array used for replacement, and $... represents other arrays used for replacement. For example:
$arr1 = array('a' => 'apple', 'b' => 'banana', 'c' => 'cherry'); $arr2 = array('a' => 'orange', 'd' => 'durian'); $result = array_replace($arr1, $arr2); print_r($result);
The output result of the above code is:
Array ( [a] => orange [b] => banana [c] => cherry [d] => durian )
As can be seen from the result, the function array_replace() replaces the value 'apple' with the key name 'a' in the array $arr1 Becomes the value 'orange' with the key name 'a' in the array $arr2.
It should be noted that when one array is used to replace another array, if there is a value with the same key name in the array, the later value will overwrite the previous value. For example:
$arr1 = array('a' => 'apple', 'b' => 'banana'); $arr2 = array('a' => 'orange', 'b' => 'cherry'); $result = array_replace($arr1, $arr2); print_r($result);
The output result of the above code is:
Array ( [a] => orange [b] => cherry )
As can be seen from the result, the function array_replace() replaces the value 'orange' with the key name 'a' in the array $arr2 The value 'apple' in the array $arr1 is removed, and the value 'cherry' with the key name 'b' is replaced with the value 'banana' in the array $arr1.
To sum up, array merging is a very common operation in PHP programming. Through the PHP array merge functions array_merge(), array_merge_recursive() and array_replace(), array merge and replacement operations can be realized to facilitate PHP programming.
The above is the detailed content of PHP function application array merging. For more information, please follow other related articles on the PHP Chinese website!