Home  >  Article  >  Backend Development  >  PHP function application array merging

PHP function application array merging

WBOY
WBOYOriginal
2023-06-20 11:24:241718browse

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.

  1. array_merge()
    array_merge() function is the most basic array merge function in PHP, which merges the elements of two or more arrays into one array. Its usage is as follows:
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.

  1. array_merge_recursive()
    Similar to the array_merge() function, the array_merge_recursive() function is also a function used to merge arrays. The difference is that it handles multidimensional arrays and recursively merges values ​​with the same key into a single array. The usage method is as follows:
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.

  1. array_replace()
    array_replace() function is also a function used to merge arrays. Unlike the array_merge() function, it overwrites the value of one array with the same key name in another array. Its usage is as follows:
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn