Home > Backend Development > PHP Problem > How to find the union of arrays in php

How to find the union of arrays in php

PHPz
Release: 2023-04-18 13:55:53
Original
817 people have browsed it

Array in PHP is a very useful data structure that can store a set of data. We can access the elements in the array through the index or key in the array. In the actual development process, we often need to perform array merging operations to merge elements in multiple arrays to form a new array. In this article, we will learn how to use the array functions in PHP to perform array merging operations and obtain the collection of multiple arrays.

1. Arrays in PHP

In PHP, an array is a very commonly used data structure. It can be used to store a set of data of the same type. Each element in the array Each has an index value or key value, and we can access the elements in the array through these values.

The following are two ways to define arrays in PHP:

1. Use the array() function to create an array

$arr ​​= array('a', 'b ', 'c');

2. Use the [] symbol to create an array

$arr ​​= ['a', 'b', 'c'];

In PHP, arrays can store any type of data, such as strings, integers, floating point numbers, Boolean values, objects, etc. Moreover, PHP also provides many useful functions to operate arrays, as shown below.

2. Array merging functions in PHP

PHP provides some very useful functions to process arrays. Merging elements in multiple arrays is a very common operation. The following is an introduction to the functions used for array merging operations in PHP:

1.array_merge() function

array_merge() function is the most common function in PHP for merging arrays. It combines two One or more arrays are combined into one array. The following is the usage of array_merge() function:

$arr1 = array('a', 'b', 'c');
$arr2 = array('d', 'e', ​​' f');
$result = array_merge($arr1, $arr2);

The above code will merge the two arrays $arr1 and $arr2 together and store the result in $result in the array.

It should be noted that the array_merge() function can only handle index arrays, not associative arrays. If you need to process associative arrays, you need to use the array_merge_recursive() function.

2.array_merge_recursive() function

The array_merge_recursive() function is similar to the array_merge() function and can also be used to merge two or more arrays. The difference is that it can merge associative arrays.

$arr1 = array('a' => 'apple', 'b' => 'ball');
$arr2 = array('c' => 'cat', 'd' => 'dog');
$result = array_merge_recursive($arr1, $arr2);

The above code will merge the two associative arrays $arr1 and $arr2 together , and store the results in the $result array.

It should be noted that the array_merge_recursive() function will merge arrays recursively, and the merge operation can be performed correctly even if other arrays are nested in the array.

3.array_combine() function

array_combine() function can combine two arrays into an associative array, where the value in the first array is used as the key value of the associative array, and the second The values ​​in the array are used as the element values ​​of the associative array. The following is the usage of array_combine() function:

$keyArr = array('a', 'b', 'c');
$valArr = array('apple', 'ball', ' cat');
$result = array_combine($keyArr, $valArr);

The above code will generate an associative array with the key names $a, $b and $c, and the corresponding The values ​​are $apple, $ball, and $cat.

4.array_replace() function

The array_replace() function can be used to perform array merging operations based on key names. It combines the elements of two or more arrays into one array and replaces the values ​​of the keys with the same name. If a key only appears in one of the arrays, the value of the key will not be replaced. The following is the usage of array_replace() function:

$arr1 = array('a' => 'apple', 'b' => 'ball');
$arr2 = array(' b' => 'bat', 'c' => 'cat');
$result = array_replace($arr1, $arr2);

The above code will associate the two The arrays $arr1 and $arr2 are merged and the element with key value $b is replaced with $bat.

5.array_intersect() function

The array_intersect() function can be used to obtain the intersection of multiple arrays, that is, only retain elements that appear in multiple arrays at the same time. The following is the usage of array_intersect() function:

$arr1 = array('a', 'b', 'c');
$arr2 = array('b', 'c', ' d');
$arr3 = array('c', 'd', 'e');
$result = array_intersect($arr1, $arr2, $arr3);

Above The code will get the intersection of the three arrays, which is the $c element.

6.array_diff() function

array_diff() function is used to obtain the difference set of multiple arrays, that is, it only appears in the first array and does not appear in other arrays. Elements. The following is the usage of array_diff() function:

$arr1 = array('a', 'b', 'c');
$arr2 = array('b', 'c', ' d');
$arr3 = array('c', 'd', 'e');
$result = array_diff($arr1, $arr2, $arr3);

The above code will get the elements that only appear in $arr1, that is, the $a element.

3. PHP Array Collection Example

With the above knowledge, we can implement a PHP code that seeks the collection of multiple arrays. The following is the code implementation:

function array_union(...$arrays) {

$res = array();
foreach($arrays as $arr) {
    $res = array_merge_recursive($res, $arr);
}
return $res;
Copy after login

}

The above code uses variable parameters (...) to Represents multiple arrays. In the function, we use a foreach loop to traverse all the incoming parameter arrays, merge them using the array_merge_recursive() function, and finally return the merged result array.

With this function, we can call it to solve the collection of multiple arrays, as follows:

$arr1 = array('a' => 'apple', ' b' => 'ball');
$arr2 = array('b' => 'bat', 'c' => 'cat', 'd' => 'dog');
$arr3 = array('c' => 'cat', 'd' => 'deer', 'e' => 'elephant');
$result = array_union($arr1, $ arr2, $arr3);

The above code will merge three associative arrays and return an associative array containing all elements.

Summary

Arrays are a very important data structure in PHP. We often need to merge multiple arrays to obtain a collection. This article introduces some useful array functions in PHP, including array_merge(), array_merge_recursive(), array_combine(), array_replace(), array_intersect(), and array_diff(). We also implemented a function to find the union of multiple arrays. Readers can choose the function that suits them to perform array operations according to their own needs.

The above is the detailed content of How to find the union of arrays in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template