Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. Find the differences betwween two arrays

Find the differences betwween two arrays

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
phpdesignhostingdata-structureshelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Marc Firth
    wrote on last edited by
    #1

    Hi all, I have a problem finding the differences between two arrays:

    $first_array = array("a"=>"1", "b"=>"7", "c"=>"4", "d"=>"4");

    $second_array = array("a"=>"1", "b"=>"10", "c"=>"10", "d"=>"4", "e"=>"7");

    I'm trying to subtract the old array from the new one and find any new values. So the output should be:

    $new_array = array("b"=>"3", "c"=>"6", "e"=>"7");

    I tried using array_diff() and array_diff_assoc(). But they give me the wrong output. Any ideas? Thanks.

    Portfolio | Web Design, Web Hosting & IT Support

    L 1 Reply Last reply
    0
    • M Marc Firth

      Hi all, I have a problem finding the differences between two arrays:

      $first_array = array("a"=>"1", "b"=>"7", "c"=>"4", "d"=>"4");

      $second_array = array("a"=>"1", "b"=>"10", "c"=>"10", "d"=>"4", "e"=>"7");

      I'm trying to subtract the old array from the new one and find any new values. So the output should be:

      $new_array = array("b"=>"3", "c"=>"6", "e"=>"7");

      I tried using array_diff() and array_diff_assoc(). But they give me the wrong output. Any ideas? Thanks.

      Portfolio | Web Design, Web Hosting & IT Support

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      I'm unaware of any library function that would do that, however you can easily iterate over the first array using

      foreach ($first_array as $key => $value) {...}

      and then look up the key in the second array, and adjust the value. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      Merry Christmas and a Happy New Year to all.


      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups