Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How php compares two arrays and returns different elements

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces php how to compare two arrays and return different elements of the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, with a certain reference value, I believe that after reading this php how to compare two arrays and return different elements of the article will have a harvest, let's take a look at it.

Comparison method: 1, use "array_diff (array 1, array 2)" statement, compare key value; 2, use "array_diff_key (array 1, array 2)" statement, compare key name; 3, use "array_diff_assoc (array 1, array 2)" statement, compare key name and key value of array at the same time.

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

Php compares two arrays and returns different elements

To compare two arrays and get different elements is to compare the array to take the difference set.

In PHP, you can use the following functions to take subtractions:

Array_diff () function

Array_diff_key () function

Array_diff_assoc () function

1. Array_diff () function-- compare the key values of an 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: returns different elements of an array

In the above example, only one key name in the $arr1 array is different from that in the $arr2 array, and the value "" c "= >" blue "is obtained by the array $arr1.

2. Array_diff_key () function-- compare the key names of the array

Array_diff_key ($arr1,$arr2...) The function only compares the key names of the array and also returns a subtractive array. The elements in the subtractive array exist in the compared array $arr1, but not in the other parameter array $arr2.... In the middle.

Example: returns different elements of an array

In the above example, there are two different key names in the $arr1 array and the $arr2 array, and the array $arr1 gives the values "" c "= >" blue "" and "d" = > "yellow".

3. Array_diff_assoc () function-compare the key name and key value 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 () and array_diff_key (), are obtained from the array $arr1 being compared.

Example: returns different elements of an array

In the above example, the $arr1 array is compared to the $arr2 array with three different elements, and then the array $arr1 yields the values "a" = > "red", "" c "= >" blue ","d" = > "yellow".

That's all for the article on "how php compares two arrays and returns different elements". Thank you for reading! I believe you all have a certain understanding of "how php compares two arrays and returns different elements". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report