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

The definition and usage of array_diff_assoc() function in php

巴扎黑
Release: 2023-03-07 21:02:02
Original
1680 people have browsed it

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>
Copy after login

Running result:

Array ( [d] => yellow )
Copy after login

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>
Copy after login

Running results

Array ( [c] => blue [d] => yellow )
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template