search
HomeBackend DevelopmentPHP TutorialDetailed 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!

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
使用C#中的Array.Sort函数对数组进行排序使用C#中的Array.Sort函数对数组进行排序Nov 18, 2023 am 10:37 AM

标题:C#中使用Array.Sort函数对数组进行排序的示例正文:在C#中,数组是一种常用的数据结构,经常需要对数组进行排序操作。C#提供了Array类,其中有Sort方法可以方便地对数组进行排序。本文将演示如何使用C#中的Array.Sort函数对数组进行排序,并提供具体的代码示例。首先,我们需要了解一下Array.Sort函数的基本用法。Array.So

用vimdiff替代svn diff:比较代码的工具用vimdiff替代svn diff:比较代码的工具Jan 09, 2024 pm 07:54 PM

在linux下,直接使用svndiff命令查看代码的修改是很吃力的,于是在网上搜索到了一个比较好的解决方案,就是让vimdiff作为svndiff的查看代码工具,尤其对于习惯用vim的人来说真的是很方便。当使用svndiff命令比较某个文件的修改前后时,例如执行以下命令:$svndiff-r4420ngx_http_limit_req_module.c那么实际会向默认的diff程序发送如下命令:-u-Lngx_http_limit_req_module.c(revision4420)-Lngx_

简单明了的PHP array_merge_recursive()函数使用方法简单明了的PHP array_merge_recursive()函数使用方法Jun 27, 2023 pm 01:48 PM

在进行PHP编程时,我们常常需要对数组进行合并。PHP提供了array_merge()函数来完成数组合并的工作,不过当数组中存在相同的键时,该函数会覆盖原有的值。为了解决这个问题,PHP在语言中还提供了一个array_merge_recursive()函数,该函数可以合并数组并保留相同键的值,使得程序的设计变得更加灵活。array_merge

如何使用PHP中的array_combine函数将两个数组拼成关联数组如何使用PHP中的array_combine函数将两个数组拼成关联数组Jun 26, 2023 pm 01:41 PM

在PHP中,有许多强大的数组函数可以使数组的操作更加方便和快捷。当我们需要将两个数组拼成一个关联数组时,可以使用PHP的array_combine函数来实现这一操作。这个函数实际上是用来将一个数组的键作为另一个数组的值,合并成一个新的关联数组。接下来,我们将会讲解如何使用PHP中的array_combine函数将两个数组拼成关联数组。了解array_comb

PHP array_fill()函数用法详解PHP array_fill()函数用法详解Jun 27, 2023 am 08:42 AM

在PHP编程中,数组是一种非常重要的数据结构,能够轻松地处理大量数据。PHP中提供了许多数组相关的函数,array_fill()就是其中之一。本篇文章将详细介绍array_fill()函数的用法,以及在实际应用中的一些技巧。一、array_fill()函数概述array_fill()函数的作用是创建一个指定长度的、由相同的值组成的数组。具体来说,该函数的语法

Python中的Array模块怎么使用Python中的Array模块怎么使用May 01, 2023 am 09:13 AM

Python中的array模块是一个预定义的数组,因此其在内存中占用的空间比标准列表小得多,同时也可以执行快速的元素级别操作,例如添加、删除、索引和切片等操作。此外,数组中的所有元素都是同一种类型,因此可以使用数组提供的高效数值运算函数,例如计算平均值、最大值和最小值等。另外,array模块还支持将数组对象直接写入和读取到二进制文件中,这使得在处理大量数值数据时更加高效。因此,如果您需要处理大量同质数据,可以考虑使用Python的array模块来优化代码的执行效率。要使用array模块,首先需要

Java中的ArrayStoreException异常的常见原因是什么?Java中的ArrayStoreException异常的常见原因是什么?Jun 25, 2023 am 09:48 AM

在Java编程中,数组是一种重要的数据结构。数组可以在一个变量中存储多个值,更重要的是可以使用索引访问每个值。但是在使用数组时,可能会出现一些异常,其中之一是ArrayStoreException。本文将讨论ArrayStoreException异常的常见原因。1.类型不匹配数组在创建时必须指定元素类型。当我们试图将不兼容的数据类型存储到一个数组中时,就会抛

PHP array_flip()函数用法介绍PHP array_flip()函数用法介绍Jun 27, 2023 am 08:24 AM

PHP语言是一种广泛应用的Web编程语言,在开发Web应用程序期间,数组是一个非常常用的数据结构。数组里的键-值对可以让程序员方便地组织和管理数据。PHP的array_flip()函数是非常有用的一个函数,它可以将数组的键和值交换,把原来的键作为值,原来的值作为键。本文将向您介绍PHP的array_flip()函数用法及其在实际编程中的应用。array_fl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!