Greenhorn PHP Comparison of two arrays

Source: Internet
Author: User
Novice! PHP about two arrays comparison!
There are now two arrays:

A array is a product attribute obtained from the product ID to the database.

b array is the product attribute submitted by the page.

Two array contrast, with a array as the benchmark, if the B array is less than an array of elements, then delete this element in the database association id!

If the B array has one more element than the a array, add the element and the correlation ID inside the database

Ask the Great God!!!
------to solve the idea----------------------
b more items than a Array_diff (b, a)
A more than B Array_diff (A, B)

------to solve the idea----------------------

b array has one less element 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
)
*/
b Array has one more element than array a
$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
)
*/

In contrast to the $ A array with the $new array, it's up to you to add or subtract.
  • 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.