Detailed explanation of array_diff() function in php

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

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

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

array_diff() function is used to compare the keys of two (or more) arrays value and returns the difference.

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

Syntax

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

Parameters                 Description

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

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

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

<?php
    $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
    $a2=array("e"=>"red","f"=>"black","g"=>"purple");
    $a3=array("a"=>"red","b"=>"black","h"=>"yellow");
    $result=array_diff($a1,$a2,$a3);
    print_r($result);
?>
Copy after login
<pre class="brush:php;toolbar:false">
<?phpheader("Content-Type:text/html;charset=utf-8");/*知识点一:array_diff — 计算数组的差集
原形:array array_diff ( array $array1 , array $array2 [, array $... ] )
功能:对比返回在 array1 中但是不在 array2 及后面参数数组中的值。
返回:返回一个数组,该数组包括了所有在 array1 中但是不在任何其它参数数组中的值。
注意:返回的数组键名保留不变。
应用背景案例分析:
假设我们做一个邮件群发系统,邮件都在数组 $a中保留。
如果有本次我们不想发送给其中特定的几个邮箱,没必要从$a中删除,因为下次还有添加比较麻烦。
处理方法:在做一个数组$b,让系统自动差集计算,不对在$b中包含的几个用户发邮件,其他都发送。 */ //例:/*$a = range(1,9);        //创建一个包含 1,2,3,4,5,6,7,8,9 的数组
$b = array(5,6,1);        //计划排除其中的 1,5,6
$c = array_diff($a,$b); //结果为  2,3,4,7,8,9*/ //例:$a = array(&#39;动&#39;, &#39;美&#39;, &#39;学&#39;, &#39;院&#39;); 
$b = array(&#39;a&#39;=>&#39;学&#39;,&#39;美&#39;);$c = array_diff($a,$b);var_export($c);     //结果为 动 院/*知识点二:array_diff_assoc — 带索引检查计算数组的差集
函数原形:array array_diff_assoc ( array $array1 , array $array2 [, array $... ] )
功能:计算差集,要求键值对完全相同的才进行减去。
返回:返回一个数组,该数组包括了所有在 array1 中但是不在任何其它参数数组中的"键值对"*/$a = array(&#39;d&#39; => &#39;动&#39;, &#39;l&#39; => &#39;美&#39;, &#39;x&#39; => &#39;学&#39;, &#39;y&#39; => &#39;院&#39;); 
$b = array(&#39;l&#39; => &#39;美&#39;,&#39;a&#39; => &#39;动&#39; );$c = array_diff_assoc($a,$b);var_export($c); 
//结果:array ( &#39;d&#39; => &#39;动&#39;, &#39;x&#39; => &#39;学&#39;, &#39;y&#39; => &#39;院&#39;, )/*知识点三:array_diff_key — 使用键名比较计算数组的差集
函数原形:array array_diff_key ( array $array1 , array $array2 [, array $... ] )
根据 array1 中的键名和 array2 进行比较,返回不同键名的项。 
本函数和 array_diff() 相同只除了比较是根据键名而不是值来进行的。*/$a = array(&#39;d&#39; => &#39;动&#39;, &#39;l&#39; => &#39;美&#39;, &#39;x&#39; => &#39;学&#39;, &#39;y&#39; => &#39;院&#39;); 
$b = array(&#39;d&#39; => &#39;学&#39;,&#39;l&#39; => &#39;美&#39;);$c = array_diff_key($a,$b);var_export($c);     //结果:array ( &#39;x&#39; => &#39;学&#39;, &#39;y&#39; => &#39;院&#39;, )/*目前关于array_diff的函数及变种一共有八个分别如下,比较实用的有三个
array_diff_assoc — 带索引检查计算数组的差集
array_diff_key — 使用键名比较计算数组的差集
array_diff_uassoc — 用用户提供的回调函数做索引检查来计算数组的差集
array_diff_ukey — 用回调函数对键名比较计算数组的差集
array_diff — 计算数组的差集
array_udiff_assoc — 带索引检查计算数组的差集,用回调函数比较数据
array_udiff_uassoc — 带索引检查计算数组的差集,用回调函数比较数据和索引
array_udiff — 用回调函数比较数据来计算数组的差集*/?>
Copy after login


The above is the detailed content of Detailed explanation of array_diff() 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