Get the App
SLTechnology News&Howtos  ›  Development  › 

How php does array comparison to get different elements

Shulou Source: shulou.com Published: 2022-06-03 18:26:59 11月12日 Update

This article will explain in detail how to compare php in arrays to get different elements. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Methods: 1, use array_diff () to compare according to array key value, syntax "array_diff (array 1, array 2)"; 2, use array_diff_assoc () to compare according to array "key / value pair", syntax "array_diff_assoc (array 1, array 2)".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Method 1: use the array_diff () function to compare the key values of the array

Array_diff ($arr1,$arr2...) The function compares only the key values of the array and returns an array of subtractions. The elements in the subtractive array exist in the compared array $arr1, but not in the other parameter array $arr2.... In the middle.

Example:

Output result:

Method 2: use the array_diff_assoc () function to compare the key names and key values of the array

Array_diff_assoc ($arr1,$arr2...) The function compares the key name and key value of the array, and also returns an array of subtractions, which, like array_diff (), are obtained from the array $arr1 being compared.

Output result:

This is the end of this article on "how to compare php in arrays to get different elements". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Arrays elements functions subtractions methods articles different two more results syntax output good practical content parameters tutorials articles environment computer Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Microsoft Docker MySQL OPPO Reno