Beginner! PHP comparison of two arrays!

Source: Internet
Author: User
Beginner! PHP comparison of two arrays! Now there are two arrays:

Array A is the product property obtained from the database based on the product ID.

Array B is the product attribute submitted by the page.

Compare the two arrays, with array A as the benchmark. if array B is less than array A, delete the associated ID of this element in the database!

If array B has one more element than array A, add this element and associated ID to the database.

Thank you !!!


Reply to discussion (solution)

Array_diff (B,)
Array_diff (A, B)

// Array B has one element less than array A $ a = array ('A1', 'A2 ', 'A3', 'A4 '); $ B = array ('A1', 'A2 ', 'A3'); $ new = array_merge (array_diff ($ a, array_intersect ($ a, $ B )), array_diff ($ B, array_intersect ($ a, $ B); echo"
"; Print_r ($ new); echo"
";/* Array ([0] => a4) * // array B has an element $ A = Array ('A1', 'A2 ', 'A3 '); $ B = array ('A1', 'A2', 'A3 ', 'b1', 'B2 '); $ new = array_merge (array_diff ($ a, array_intersect ($ a, $ B), array_diff ($ B, array_intersect ($ a, $ B); echo"
"; Print_r ($ new); echo"
";/* Array ([0] => b1 [1] => b2 )*/

Compare the $ new array with the $ a array. whether to add or delete the array depends on yourself.

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.