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.