In php, if I want to perform operations on two arrays, such as union, intersection, and difference set, we can directly use the built-in functions of php to perform operations such as array_merge (), array_intersect (), and array_dif.
In php, if I want to perform operations on two arrays, such as union, intersection, and difference set, we can directly use the built-in functions of php to perform operations such as array_merge (), array_intersect (), array_diff ().
Differences between the combined array_merge and "+" of the calculated array
The array_merge () function combines two or more numbers into an array.
If the key name is repeated, the key value of the key is the value corresponding to the last key name (the latter overwrites the previous one). If the array is digitally indexed, the key name will be re-indexed in a continuous manner.
Note:If you only input an array to the array_merge () function and the key name is an integer, the function returns a new array with an integer key name, its key name starts from 0 and is re-indexed. the code is as follows:
- $ A = array (1 => 'A', 'B', 'C ');
- $ B = array (1 => 'A', 2, 'C ');
- $ Union = array_merge ($ a, $ B );
- $ Plus = $ a + $ B;
- Print_r ($ union );
- Print_r ($ plus );
- // The result is as follows:
- Array
- {
- [0] =>
- [1] => B
- [2] => c
- [3] => aa
- [4] => 2
- [5] => c
- }
- Array
- (
- [1] =>
- [2] => B
- [3] => c
- )
When the two arrays to be merged have the same string key, using array_merge () will overwrite the original value, when "+" is used to merge an array, the first value will be returned as the final result, just like the number key of the array merged with "+", as shown in the following example:
- $ A2 = array ('str' => 'A', 'B', 'C ');
- $ B2 = array ('str' => 'A', 2, 'C ');
- $ Union2 = array_merge ($ a2, $ b2 );
- $ Plus2 = $ a2 + $ b2;
- Print_r ($ union2 );
- Print_r ($ plus2 );
- // The result is as follows:
- Array
- (
- [Str] => aa
- [0] => B
- [1] => c
- [2] => 2
- [3] => c
- )
- Array
- (
- [Str] =>
- [0] => B
- [1] => c
- )
Note:If you want to use array_merge to merge two arrays, the returned results may have the same elements. in this case, you can use array_unique () to remove the same elements.
Calculate the intersection of arrays
The array_intersect () function returns an intersection array of two or more arrays. The result array contains all values in the compared array and all other parameter arrays, the key name remains unchanged. note: only values are used for comparison. the code is as follows:
- $ A = array ('jpg ', 'PNG', 'GIF', 'bmp ');
- $ B = array ('jpg ', 'txt', 'docx', 'bmp ');
- $ Intersection = array_intersect ($ a, $ B );
You can also use functions to obtain what you want (for example, the elements are case-insensitive). The code is as follows:
- $ Intersection2 = array_intersect (array_map ('strlower ', $ a), array_map ('strlower', $ B ));
- Print_r ($ intersection );
- Print_r ($ intersection2 );
- // The result is as follows:
- Array
- (
- [3] => bmp
- )
- Array (
- [0] => jpg
- [3] => bmp
- )
Calculate the difference set of the array,The code is as follows:
- $ Old = array ('jpg ', 'PNG', 'GIF', 'bmp ');
- $ New = array ('jpg ', 'txt', 'docx', 'bmp ');
- $ Difference = array_diff ($ old, $ new );
Note: The Returned result element contains $ old, excluding $ new.
Print_r ($ difference );
Result:
- Array
- (
- [0] => jpg
- [1] => png
- [2] => gif
- )
You can also use the function to perform processing first and then calculate the difference set.
The array_diff () function returns the number of difference sets of two arrays. This array includes all the key values in the compared array, but not in any other parameter array. in the returned array, the key names remain unchanged.
Syntax: array_diff (array1, array2, array3 ...)
The code is as follows:
- $ Difference = array_diff (array_map ('strtolower', $ old ),
- Array_map ('strlower ', $ new ));