Detailed explanation of array_diff_assoc() function in php
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); ?>
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...);
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); ?>
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); ?>
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()
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()
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

Under Linux, it is very difficult to directly use the svndiff command to view code modifications, so I searched for a better solution on the Internet, which is to use vimdiff as a code viewing tool for svndiff, especially for those who are accustomed to using vim. It is very convenient. When using the svndiff command to compare the modifications of a certain file, for example, if you execute the following command: $svndiff-r4420ngx_http_limit_req_module.c, the following command will actually be sent to the default diff program: -u-Lngx_http_limit_req_module.c(revision4420)-Lngx_

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

In PHP programming, array is a very important data structure that can handle large amounts of data easily. PHP provides many array-related functions, array_fill() is one of them. This article will introduce in detail the usage of the array_fill() function, as well as some tips in practical applications. 1. Overview of the array_fill() function The function of the array_fill() function is to create an array of a specified length and composed of the same values. Specifically, the syntax of this function is

The array module in Python is a predefined array, so it takes up much less space in memory than a standard list, and can also perform fast element-level operations such as adding, deleting, indexing, and slicing. In addition, all elements in the array are of the same type, so you can use the efficient numerical operation functions provided by the array, such as calculating the average, maximum, and minimum values. In addition, the array module also supports writing and reading array objects directly into binary files, which makes it more efficient when processing large amounts of numerical data. Therefore, if you need to process a large amount of homogeneous data, you may consider using Python's array module to optimize the execution efficiency of your code. To use the array module, you first need to

In Java programming, array is an important data structure. Arrays can store multiple values in a single variable, and more importantly each value can be accessed using an index. But while working with arrays, some exceptions may occur, one of them is ArrayStoreException. This article will discuss common causes of ArrayStoreException exceptions. 1. Type mismatch The element type must be specified when the array is created. When we try to store incompatible data types into an array, it throws

In PHP programming, array is a frequently used data type. There are also quite a few array operation functions, including the array_change_key_case() function. This function can convert the case of key names in the array to facilitate our data processing. This article will introduce how to use the array_change_key_case() function in PHP. 1. Function syntax and parameters array_change_ke
