PHP array to remove the same item

Source: Internet
Author: User
$a = Array (    "id" =>7, "title" = "A"),    Array ("id" =>5, "title" = "Z"), $b = Array (    array (" ID "=>5", "title" = "A"),    Array ("id" =>1, "title" = "Z"),    Array ("id" =>2, "title" = "Z"),);// Remove the item//Result $b = Array ("id"    =>1, "title" = "Z"),    Array ("id" =>2, "title" = "Z") in the $a of the ID in the $b,);


Reply to discussion (solution)

$a = Array (    "id" =>7, "title" = "A"),    Array ("id" =>5, "title" = "Z"), $b = Array (    array (" ID "=>5", "title" = "A"),    Array ("id" =>1, "title" = "Z"),    Array ("id" =>2, "title" = "Z"),); foreach ($b as $k = $v)  foreach ($a as $t) if ($v [' id '] = = $t [' id ']) unset ($b [$k]);p rint_r ($b);
Array ([    1] = = Array        (            [id] = 1            [title] = Z        )    [2] = = Array        (            [id] = 2< C8/>[title] (= z        ))

$a = Array (    "id" =>7, "title" = "A"),    Array ("id" =>5, "title" = "Z"), $b = Array (    Array ("id" =>5, "title" = "A"),    Array ("id" =>1, "title" = "Z"),    Array ("id" =>2, "title" = " Z "),); foreach ($b as $k = $v)  foreach ($a as $t) if ($v [' id '] = = $t [' id ']) unset ($b [$k]);p rint_r ($b);
Array (    [1] = = Array        (            [id] = 1            [title] = Z        )    [2] = = Array        (            [id] = 2            [title] = Z        ))



Remove an item during a loop, and the next loop is not affected?

foreach is unaffected
For affected

  • 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.