Summary of the use of PHP Array_diff and ARRAY_DIFF_ASSOC

Source: Internet
Author: User

Summary of the use of PHP Array_diff and Array_diff_assoc2015-11-07 17:01 184 people read Comments (0) favorite reports Classification:PHP Learning Impressions (1)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Well, this is the first time I've written csdn blog, allowing excitement.

Below is I in the development of a mall system encountered using Array_diff some of the summary, write to share, also as their own review it, the process is very wordy, please crossing patience.

One: Using Array_diff and ARRAY_DIFF_ASSOC Prelude

In the mall development, often need to use the data additions and deletions to check. Where the data is updated, many times we only need to update one of the fields, or some of the fields, without needing to update all the fields together, then we need to find out what needs to be updated and which ones don't need to be updated. Both Array_diff and ARRAY_DIFF_ASSOC can check the difference of the array, and we simply compare the old data array obtained from the database with the new array of data submitted, and both Array_diff and ARRAY_DIFF_ASSOC can return the differential group.

II: Learn Array_diff and ARRAY_DIFF_ASSOC

a Rray_diff ()

Array Array_diff ( array $array1 , array $array2 [, array $... ]), returns an array of all the different values in the array1 that are associated with other arrays In addition, the corresponding key name is reserved. However, the function can only compare the difference of the first dimension of a multidimensional array. And this contrast only to the key value of the contrast, regardless of the key name (will only find two (or more than two) the value of the key value of the array is different).

Example: Array_diff can find the difference between two arrays c_pid:

However, if any of the key values of the array overlap with the changed values, Array_diff cannot measure this change value, as follows:

In the normal case of array1 and array2 comparisons, the updated elements have c_pid and C_order, but the results only get the c_order difference, why?

Personal understanding: Array_diff () compares the value of array1 with the value of array2, regardless of the key name, so the C_pid value of array1 is found in Array2 c_level, so the difference between c_pid is ignored.


ARRAY_DIFF_ASSOC ()
This is not detailed, it and Array_diff () usage, the difference is that it is the comparison with the key name, that is, it is found in several arrays in the key name of the same key value different items, that is, the second case above Array_diff it can find C_pid and c_ Order two difference values. If you don't believe, you can try it crossing. Younger brother Novice A, everything beginning difficult, the first time to write blog, even if a little dish, hope each road big God Haihan.

---------2015.11.7

Summary of the use of PHP Array_diff and ARRAY_DIFF_ASSOC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.