Home  >  Article  >  Backend Development  >  The definition and usage of array_diff_assoc() function in php

The definition and usage of array_diff_assoc() function in php

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

Definition and usage

array_diff_assoc() function is used to compare the key names and key values ​​of two (or more) arrays and return the difference set.

This function compares the key names and key values ​​​​of two (or more) arrays, and returns a difference array, which includes everything in the compared array (array1), but not in any other The key name and key value in the parameter array (array2 or array3, etc.).

Example:

Compare the keys and values ​​of two arrays and return the difference set:

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

Running result:

Array ( [d] => yellow )

Syntax

array_diff_assoc(array1,array2,array3...);

Parameter introduction:

array1 is 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.

Return value: Returns an array that contains all key names and key values ​​that are in array1 but are not in any other parameter array (array2 or array3, etc.).

Example:

Compare the keys and values ​​of three arrays and return the difference set:

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

Running results

Array ( [c] => blue [d] => yellow )

The above is the detailed content of The definition and usage of array_diff_assoc() function in php. 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