Home > Backend Development > PHP Tutorial > PHP array introductory tutorial: finding the difference set of associative arrays

PHP array introductory tutorial: finding the difference set of associative arrays

WBOY
Release: 2016-07-25 08:57:54
Original
1162 people have browsed it
This article introduces the method of finding associative array differences in PHP arrays, mainly the usage of PHP array function array_diff_assoc. Friends in need can refer to it.

In php, find the difference set of associative arrays.

The function array_diff_assoc() is basically the same as array_diff() except that it also considers the keys of the array when comparing. Therefore, only key/value pairs that appear in the first array but not in the other input arrays are returned in the result array.

The form is as follows: array array_diff_assoc(array array1,array array2[,arrayN…])

Example, only [yellow] => Banana is returned, because this special key/value pair appears in $fruit1, but does not exist in $fruit2 and $fruit3.

<?php
/**
* 求关联数组的差集
* by bbs.it-home.org
*/
$fruit1 = array("red"=>"Apple","yellow"=>"Banana","orange"=>"Orange");  
$fruit2 = array("yellow"=>"Pear","red"=>"Apple","purple"=>"Grape");  
$fruit3 = array("green"=>"Watermelon","orange"=>"Orange","red"=>"Apple");  
$intersection = array_diff_assoc($fruit1, $fruit2, $fruit3);  
print_r($intersection);  
  
// output  
// Array ( [yellow] => Banana )  
?>
Copy after login

Instructions: When traversing an array in PHP, you usually want to traverse the array and get each key or value (or get both the key and the value). PHP provides some functions to meet your needs. Many functions perform two tasks, not only obtain the key or value at the current pointer position, but also move the pointer to the next appropriate position.



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