Detailed explanation of array_diff_assoc() function in php

PHP中文网
Release: 2023-03-16 19:52:02
Original
4155 people have browsed it

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

<?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);
?>
Copy after login

Definition and usage

array_diff_assoc() function is used for comparison Key names and key values ​​of two (or more) arrays and return the difference.

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.).

Syntax

array_diff_assoc(array1,array2,array3...);
Copy after login

Parameters                                                                                                                                                                                                           . 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.

Returns a difference array that includes all key names and key values ​​that are in the compared array (array1) but are not in any other parameter array (array2 or array3, etc.).

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

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_diff_assoc($a1,$a2);
print_r($result);
?>
Copy after login

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

<?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);
?>
Copy after login

1: Prelude to using array_diff and array_diff_assoc

In mall development, it is often necessary to add, delete, modify and check data. Among them, when updating data, many times we only need to update one field or some fields, and do not need to update all fields together. So here we need to find out which ones need to be updated and which ones do not need to be updated. Both array_diff and array_diff_assoc can check the difference of the array. We only need to compare the old data array to be updated obtained from the database with the new data array submitted. Both array_diff and array_diff_assoc can return the difference array.

Two: Learn array_diff and array_diff_assoc

array_diff()
Copy after login

array array_diff(array $array1, array $array2 [, array $...]), returns an array, the array includes all array1 For elements with different values ​​in other arrays, the corresponding key names are retained. However, this function can only perform difference comparisons on the first dimension of multidimensional arrays. Moreover, this comparison only compares key values, and has nothing to do with key names (it will only find values ​​with different key values ​​in two (or more) arrays).

Example: array_diff can find the difference c_pid between two arrays:

However, if any key value of the array overlaps with the changed value, array_diff cannot detect the changed value, as follows:

Under normal circumstances, in the comparison between array1 and array2, the updated elements are c_pid and c_order, but the result only gets the difference of c_order. Why?

Personal understanding: array_diff() compares the value of array1 with the value of array2, regardless of the key name, so the value of c_pid of array1 is found in the c_level of array2, so the difference in c_pid is ignored.

array_diff_assoc()
Copy after login

I won’t go into details about this. It is used the same as array_diff(). The difference is that its comparison is with key names, which means that what it finds is that the key names in several arrays are the same. Items with different key values, that is to say, in the second case of array_diff above, it can find the difference between c_pid and c_order. If you don’t believe it, you can give it a try. I am a newbie, everything is difficult at the beginning. It is my first time to write a blog post, even if I am ordering dishes, I hope that all the masters will understand.


The above is the detailed content of Detailed explanation of array_diff_assoc() function in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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