- Trang chủ
- Phát triển web
- PHP
- Hàm array_diff[] trong PHP
Hướng dẫn cách sử dụng hàm array_diff[] về mảng trong lập trình PHP
Tác dụng của hàm array_diff[]
The array_diff[]
function compares the values of two or more arrays and returns the differences.
The following table summarizes the technical details of this function.
Returns an array containing all the elements from array1 that are not present in any of the other arrays. |
PHP 4.0.1+ |
Syntax
The basic syntax of the array_diff[]
function is given with:
array_diff[array1, array2, ...];
The following example shows the array_diff[]
function in action.
Parameters
The array_diff[]
function accepts the following parameters.
array1 | Required. Specifies the array to compare from. |
array2 | Required. Specifies an array to compare against. |
... | Optional. Specifies more arrays to compare against. |
More Examples
Here're some more examples showing how array_diff[]
function actually works:
The following example shows how to use this function to compare an array against two other arrays.
Two elements are considered equal if their string representation are same, i.e., [string] $elem1 === [string] $elem2. Let's take a look at the following example:
You can also use associative arrays, however the keys are not considered in the comparison.