Home>Article>Backend Development> Detailed explanation of array_diff() function in php

Detailed explanation of array_diff() function in php

PHP中文网
PHP中文网 Original
2017-10-27 09:38:41 1933browse

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

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

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

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:

"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); ?>
 '学','美');$c = array_diff($a,$b);var_export($c); //结果为 动 院/*知识点二:array_diff_assoc — 带索引检查计算数组的差集 函数原形:array array_diff_assoc ( array $array1 , array $array2 [, array $... ] ) 功能:计算差集,要求键值对完全相同的才进行减去。 返回:返回一个数组,该数组包括了所有在 array1 中但是不在任何其它参数数组中的"键值对"*/$a = array('d' => '动', 'l' => '美', 'x' => '学', 'y' => '院'); $b = array('l' => '美','a' => '动' );$c = array_diff_assoc($a,$b);var_export($c); //结果:array ( 'd' => '动', 'x' => '学', 'y' => '院', )/*知识点三:array_diff_key — 使用键名比较计算数组的差集 函数原形:array array_diff_key ( array $array1 , array $array2 [, array $... ] ) 根据 array1 中的键名和 array2 进行比较,返回不同键名的项。 本函数和 array_diff() 相同只除了比较是根据键名而不是值来进行的。*/$a = array('d' => '动', 'l' => '美', 'x' => '学', 'y' => '院'); $b = array('d' => '学','l' => '美');$c = array_diff_key($a,$b);var_export($c); //结果:array ( 'x' => '学', 'y' => '院', )/*目前关于array_diff的函数及变种一共有八个分别如下,比较实用的有三个 array_diff_assoc — 带索引检查计算数组的差集 array_diff_key — 使用键名比较计算数组的差集 array_diff_uassoc — 用用户提供的回调函数做索引检查来计算数组的差集 array_diff_ukey — 用回调函数对键名比较计算数组的差集 array_diff — 计算数组的差集 array_udiff_assoc — 带索引检查计算数组的差集,用回调函数比较数据 array_udiff_uassoc — 带索引检查计算数组的差集,用回调函数比较数据和索引 array_udiff — 用回调函数比较数据来计算数组的差集*/?>


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!

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