In the PHP programming language, arrays are one of the more commonly used data types. Arrays can store an ordered set of values, and can also be used to implement data structures such as sets, stacks, and queues. This article will discuss the set operations of PHP arrays, including array deduplication, array merging, array intersection and difference operations.
In actual development, we sometimes need to remove duplicate elements in the array. PHP provides some built-in functions to implement this function, such as the array_unique() function. However, when this function handles associative arrays, it retains the last duplicate key-value pair. Therefore, we can write our own function to implement the deduplication operation:
function removeDuplicates($arr) { $output = array(); foreach($arr as $value) { if(!in_array($value, $output)) { array_push($output, $value); } } return $output; }
In the above code, we use a foreach loop to traverse the array. If it is detected that the current value does not exist in the output array, add it Go in. This function returns a new array without duplicate elements.
In actual development, we sometimes need to merge two or more arrays into a new array. PHP provides built-in functions array_merge(), array_replace(), array_merge_recursive() and other functions that can implement this function. Let's take a look at how to use these functions:
// array_merge()函数 $arr1 = array('apple', 'banana', 'orange'); $arr2 = array('pear', 'grape', 'watermelon'); $newArr = array_merge($arr1, $arr2); // 输出:apple, banana, orange, pear, grape, watermelon // array_replace()函数 $arr1 = array('apple' => 1, 'banana' => 2); $arr2 = array('orange' => 3, 'banana' => 4); $newArr = array_replace($arr1, $arr2); // 输出:apple => 1, banana => 4, orange => 3 // array_merge_recursive()函数 $arr1 = array('apple' => 1, 'banana' => 2); $arr2 = array('orange' => 3, 'banana' => array('x', 'y')); $newArr = array_merge_recursive($arr1, $arr2); // 输出:apple => 1, banana => [2, ['x', 'y']], orange => 3
When using different array merging functions, you need to pay attention to their differences and usage.
In actual development, we sometimes need to take the intersection of two or more arrays, that is, only retain the common elements among them. PHP provides built-in functions array_intersect(), array_intersect_assoc(), array_uintersect() and other functions that implement this function. Let's take a look at the use of these functions:
// array_intersect()函数 $arr1 = array('apple', 'banana', 'orange'); $arr2 = array('pear', 'banana', 'watermelon'); $newArr = array_intersect($arr1, $arr2); // 输出:banana // array_intersect_assoc()函数 $arr1 = array('apple' => 1, 'banana' => 2, 'orange' => 3); $arr2 = array('pear' => 4, 'banana' => 5, 'watermelon' => 6); $newArr = array_intersect_assoc($arr1, $arr2); // 输出:banana => 2 // array_uintersect()函数 function compare($a, $b) { if($a === $b) { return 0; } else { return ($a > $b ? 1 : -1); } } $arr1 = array('apple', 'banana', 'orange'); $arr2 = array('pear', 'banana', 'watermelon'); $newArr = array_uintersect($arr1, $arr2, 'compare'); // 输出:banana
When using different array intersection functions, you need to pay attention to their differences and usage.
In actual development, we sometimes need to take the difference set of two or more arrays, that is, remove the second array from the first array elements that appear in the array. PHP provides built-in functions array_diff(), array_diff_assoc(), array_udiff() and other functions that implement this function. Let’s take a look at the use of these functions:
// array_diff()函数 $arr1 = array('apple', 'banana', 'orange'); $arr2 = array('pear', 'banana', 'watermelon'); $newArr = array_diff($arr1, $arr2); // 输出:apple, orange // array_diff_assoc()函数 $arr1 = array('apple' => 1, 'banana' => 2, 'orange' => 3); $arr2 = array('pear' => 4, 'banana' => 5, 'watermelon' => 6); $newArr = array_diff_assoc($arr1, $arr2); // 输出:apple => 1, banana => 2, orange => 3 // array_udiff()函数 function compare($a, $b) { if($a === $b) { return 0; } else { return ($a > $b ? 1 : -1); } } $arr1 = array('apple', 'banana', 'orange'); $arr2 = array('pear', 'banana', 'watermelon'); $newArr = array_udiff($arr1, $arr2, 'compare'); // 输出:apple, orange
When using different array difference functions, you need to pay attention to their differences and usage.
Summary
In PHP programming, arrays are one of the most commonly used data types. By using some built-in functions or writing our own functions, we can easily implement various operations on PHP arrays, such as deduplication, merging, intersection, difference, etc. In actual development, these operations need to be flexibly used according to specific scenarios to achieve better results.
The above is the detailed content of How to find the set of php array. For more information, please follow other related articles on the PHP Chinese website!