Home >Backend Development >PHP Tutorial >A brief analysis of the new Array function in PHP4_PHP tutorial
The array class function of php4 has three new members;
They are: array_unique(), array_intersect() and array_diff().
As the names suggest, these three functions are very simple:
1. array_unique(array array) ------- Remove duplicate elements from the array and then return the array
eg:
$arr = array("a","b","c","b");
$arr = array_unique($arr);
var_dump($arr); -- --Show $arr only contains three elements "a", "b", "c"
2. array_intersect(array array1,array,array2 .....) ---- In array form Returns common elements in multiple arrays
eg:
$arr1 = array("a","b","c","d");
$arr2 = array("e" ,"f","b","a");
$arr = array_unique($arr);
var_dump($arr); ----Show $arr contains two elements "a", "b"
3. array_diff(array array1, array array2) ----- Returns the elements that array1 does not have compared to array2 in array form.
eg:
$arr1 = array("a","b","c","d");
$arr2 = array("e","f","b", "a");
$arr = array_unique($arr);
var_dump($arr); ----Show $arr contains two elements "c", "d" I feel php4 is more user-friendly