Home  >  Article  >  Backend Development  >  What is the php array union function?

What is the php array union function?

PHPz
PHPzOriginal
2023-04-26 09:10:14496browse

PHP is a very powerful programming language that can be used to develop various types of applications, including web applications. In PHP, an array is a very basic data type that can be used to store a set of related values. When developing applications, sometimes you need to merge multiple arrays. In this case, you can use the array union function in PHP.

  1. array_merge() function

array_merge() function is one of the most basic array merge functions in PHP. It is used to merge two or more arrays into one array. The syntax of this function is as follows:

array_merge(array1,array2,array3,...)

The array1, array2, array3 and other parameters are the arrays to be merged. The return value of this function is the merged array. If the same key name exists, the later value will overwrite the previous value.

For example:

$array1 = array('a', 'b', 'c');
$array2 = array('d', 'e', 'f');
$merged_array = array_merge($array1, $array2);
print_r($merged_array);

The output result is:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
)
  1. array_replace() function

array_replace() function and array_merge() Similar to the function, you can also merge two or more arrays into one array. But the difference is that the array_replace() function can replace the same key name instead of overwriting it directly.

The syntax of this function is as follows:

array_replace(array1,array2,array3,...)

The parameters such as array1, array2, and array3 are required Merged arrays. The return value of this function is the merged array. If the same key name exists, the later value will replace the previous value.

For example:

$array1 = array('a' => 1, 'b' => 2, 'c' => 3);
$array2 = array('b' => 4, 'c' => 5, 'd' => 6);
$replaced_array = array_replace($array1, $array2);
print_r($replaced_array);

The output result is:

Array
(
    [a] => 1
    [b] => 4
    [c] => 5
    [d] => 6
)

You can see that the values ​​corresponding to the 'b' and 'c' key names in the array $array2 have replaced the array The value in $array1. The other key names and values ​​remain unchanged.

  1. array_intersect() function

The array_intersect() function is used to calculate the intersection of multiple arrays. The syntax of this function is as follows:

array_intersect(array1,array2,array3,...)

The parameters of this function can be two or more arrays, and its return value is Array containing intersection elements.

For example:

$array1 = array('a', 'b', 'c');
$array2 = array('b', 'c', 'd');
$array3 = array('c', 'd', 'e');
$intersect_array = array_intersect($array1, $array2, $array3);
print_r($intersect_array);

The output result is:

Array
(
    [2] => c
)

It can be seen that the intersection of arrays $array1, $array2 and $array3 is 'c'.

  1. array_diff() function

array_diff() function is used to calculate the difference set of multiple arrays. The syntax of this function is as follows:

array_diff(array1,array2,array3,...)

The parameters of this function can be two or more arrays, and its return value is An array containing the elements of the difference set.

For example:

$array1 = array('a', 'b', 'c');
$array2 = array('b', 'c', 'd');
$array3 = array('c', 'd', 'e');
$diff_array = array_diff($array1, $array2, $array3);
print_r($diff_array);

The output result is:

Array
(
    [0] => a
)

It can be seen that except for the intersection element 'c' in the array $array1, the remaining elements are all in other does not exist in the two arrays, so the difference is 'a'.

  1. array_unique() function

array_unique() function is used to remove duplicate elements in an array. The syntax of this function is as follows:

array_unique(array)

The array parameter is the array from which duplicate elements are to be removed.

For example:

$array = array('a', 'a', 'b', 'c', 'c');
$unique_array = array_unique($array);
print_r($unique_array);

The output result is:

Array
(
    [0] => a
    [2] => b
    [3] => c
)

As you can see, only one of the repeated elements 'a' and 'c' is retained.

The above is the detailed content of What is the php array union function?. 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