Home  >  Article  >  Backend Development  >  Definition and usage of php array_diff function

Definition and usage of php array_diff function

巴扎黑
巴扎黑Original
2017-05-23 18:02:201933browse

array_diff() Function meaning:

array_diff() function returns the difference array of two arrays. This array contains all keys that are in the array being compared, but are not in any of the other parameter arrays. In the returned array, the key names remain unchanged.

For example:

<!DOCTYPE html>
<html>
<body><?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_diff($a1,$a2);
print_r($result);
?></body>
</html>

Run result:

Array ( [d] => yellow )

Syntax:

##array_diff(array1,array2,array3.. .);

Parameter explanation:

array1 Required. The first array to compare with other arrays. ​

array2 Required. The array to compare to the first array.

array3,... Optional. Additional array to compare with the first array.

Tips and Notes

Tips: One or more arrays can be compared with the first array.

Note: Only values ​​are used for comparison.

Return value: Returns the difference array, which includes all key values ​​that are in the compared array (array1) but are not in any other parameter array (array2 or array3, etc.).

<!DOCTYPE html>
<html>
<body><?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"black","g"=>"purple");
$a3=array("a"=>"red","b"=>"black","h"=>"yellow");
$result=array_diff($a1,$a2,$a3);
print_r($result);
?></body>
</html>

Run result:

Array ( [b] => green [c] => blue )

The above is the detailed content of Definition and usage of php array_diff function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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