Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: PHP Find Diffrence from 2 Arrays  (Read 9442 times)

0 Members and 1 Guest are viewing this topic.

irshad

    Topic Starter


    Greenhorn

    • Experience: Beginner
    • OS: Windows 7
    PHP Find Diffrence from 2 Arrays
    « on: August 17, 2014, 03:57:24 AM »
    $array1 = array('1', '2', '2', '3', '3', '4', '5');
    $array2 = array('1', '2', '3');

    output result required as:

    2, 3, 4, 5


    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: PHP Find Diffrence from 2 Arrays
    « Reply #1 on: August 17, 2014, 05:54:46 AM »
    Not sure why the difference would be 2,3,4,5. 1,2,3 are common to both arrays so would not 4,5 be the difference? I came up with this after a little research:

    Code: [Select]
    <?php
    $array1 
    = array('1''2''2''3''3''4''5');
    $array2 = array('1''2''3');
    $result array_diff($array1$array2);
    print_r($result);
    ?>


    Output:
    Code: [Select]
    Array
    (
        [5] => 4
        [6] => 5
    )

     8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    irshad

      Topic Starter


      Greenhorn

      • Experience: Beginner
      • OS: Windows 7
      Re: PHP Find Diffrence from 2 Arrays
      « Reply #2 on: August 17, 2014, 09:55:28 PM »
      thanks dear, but array_diff is not my solution, please write my perfect desired result?
      2,3,4,5

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: PHP Find Diffrence from 2 Arrays
      « Reply #3 on: August 18, 2014, 02:46:38 PM »
      Good Grief. Never seen a language so hung up on it's own punctuation.

      Code: [Select]
      <?php

      $array1 
      = array('1''2''2''3''3''4''5');
      $array2 = array('1''2''3');

      $diff1 = array();
      $diff2 = array();

      for (
      $i=0$i count($array1); $i++){
        
      $key array_search($array1[$i], $array2);
        if (
      $key != False) {
          
      $diff1[] = $array1[$i];
          unset(
      $array2[$key]);
        }
      }
      // Reset Arrays
      //
      $array1 = array('1''2''2''3''3''4''5');
      $array2 = array('1''2''3');

      $diff2 array_diff($array1$array2);
      $final array_merge($diff1$diff2);

      foreach(
      $final as $item) {
        print 
      $item." ";
      }
      ?>


      This will produce the results you're looking for. It's written as a top-down procedural and it shows. There are better tools for this but not all can run in a web page.

       8)
      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      irshad

        Topic Starter


        Greenhorn

        • Experience: Beginner
        • OS: Windows 7
        Re: PHP Find Diffrence from 2 Arrays
        « Reply #4 on: August 19, 2014, 05:28:38 AM »
        thanks again, but i am not satisfied ... change any array value and see result
        please > >
        unset array1 values (not keys) who shown in array2

        camerongray



          Expert
        • Thanked: 306
          • Yes
          • Cameron Gray - The Random Rambings of a Computer Geek
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Mac OS
        Re: PHP Find Diffrence from 2 Arrays
        « Reply #5 on: August 19, 2014, 06:32:50 AM »
        We do not do homework for people here.  The entire purpose of homework is so that you can learn how to write code yourself, getting someone else to provide the code for you will not teach you anything.  Why not look at the existing problem and work out how to do it yourself, surely that is the whole point of programming...

        Hint:  You need to go through the second array and for each element in it, remove the first occurrence of that value from the first array.

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: PHP Find Diffrence from 2 Arrays
        « Reply #6 on: August 19, 2014, 09:38:44 AM »
        We do not do homework for people here.

        Of course we do. Some of us just don't always recognize it. In any case this has been a learning experience for me as well as the OP, so to finish what was started:

        Code: [Select]
        <?php

        $array1 
        = array('1''2''2''3''3''4''5');
        $array2 = array('1''2''3');

        for (
        $i=0$i count($array1); $i++){
          
        $key array_search($array1[$i], $array2);
          if (
        $key !== False) {
             unset(
        $array2[$key]);
             unset(
        $array1[$i]);
          }
        }

        foreach(
        array_merge($array1$array2) as $item) {
          print 
        $item." ";
        }

        ?>


        For additional reference, check out this site.

         8)
        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        camerongray



          Expert
        • Thanked: 306
          • Yes
          • Cameron Gray - The Random Rambings of a Computer Geek
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Mac OS
        Re: PHP Find Diffrence from 2 Arrays
        « Reply #7 on: August 19, 2014, 09:44:25 AM »
        Of course we do. Some of us just don't always recognize it.
        The difference is between "helping with homework" and "doing it for someone" - The former is fine, it helps the person learn, simply giving them code to do exactly what they need teaches them nothing.

        Notice how I gave the person a hint as to how to start the program.  If we just give out full answers to homework questions, all that tends to happen is that the person takes the code and hands it in without even trying to learn how it works.

        irshad

          Topic Starter


          Greenhorn

          • Experience: Beginner
          • OS: Windows 7
          Re: PHP Find Diffrence from 2 Arrays
          « Reply #8 on: August 19, 2014, 10:44:39 PM »
          thanks again, yes i am trying to generate code but i am failed ... your guidance is helpful for me.

          here if change the values of array2 and result will be disturbed ...

          if you interested with me and my code and want to know what am i doing ... i tell you in detail ... thanks again
          <?php

          $array1 = array('1','2','3','4','5');
          $array2 = array('3','4','5');

          for ($i=0; $i < count($array1); $i++){
            $key = array_search($array1[$i], $array2);
            if ($key !== False) {
               unset($array2[$key]);
               unset($array1[$i]);
            }
          }

          foreach(array_merge($array1, $array2) as $item) {
            print $item." ";
          }

          ?>

          Sidewinder



            Guru

            Thanked: 139
          • Experience: Familiar
          • OS: Windows 10
          Re: PHP Find Diffrence from 2 Arrays
          « Reply #9 on: August 20, 2014, 09:51:20 AM »
          if you interested with me and my code and want to know what am i doing ... i tell you in detail ... thanks again

          OK. Let's hear it.

          Code: [Select]
          <?php

          //$array1 = array('1', '2', '2', '3', '3', '4', '5');
          //$array2 = array('1', '2', '3');

          $array1 = array('1','2','3','4','5');
          $array2 = array('3','4','5');
          $diff = array();

          foreach (
          $array1 as $item) {
            
          $key array_search($item$array2);
            if (
          $key !== False) {
                unset(
          $array2[$key]);
            } else {
                
          $diff[] = $item;
            }
          }

          foreach(
          array_merge($diff$array2) as $item) {
            print 
          $item." ";
          }

          ?>


           8)
          The true sign of intelligence is not knowledge but imagination.

          -- Albert Einstein

          irshad

            Topic Starter


            Greenhorn

            • Experience: Beginner
            • OS: Windows 7
            Re: PHP Find Diffrence from 2 Arrays
            « Reply #10 on: August 22, 2014, 11:14:25 PM »
            thanks again, but result is not correct,

            if array1 and array2 values same then see the result, (required result is 0)

            $array1 = ('1', '2', '3', '4', '5');
            $array2 = ('1', '2', '3', '4', '5');

            BC_Programmer


              Mastermind
            • Typing is no substitute for thinking.
            • Thanked: 1140
              • Yes
              • Yes
              • BC-Programming.com
            • Certifications: List
            • Computer: Specs
            • Experience: Beginner
            • OS: Windows 11
            Re: PHP Find Diffrence from 2 Arrays
            « Reply #11 on: August 22, 2014, 11:45:36 PM »
            Have you considered actually learning PHP? It's probably a lot easier to freelance that way.
            I was trying to dereference Null Pointers before it was cool.

            Sidewinder



              Guru

              Thanked: 139
            • Experience: Familiar
            • OS: Windows 10
            Re: PHP Find Diffrence from 2 Arrays
            « Reply #12 on: August 23, 2014, 05:00:33 AM »
            Your first post showed you wanted a list of the different elements from each array. Now you have two equal arrays, which should show no different elements, but for which you expect an result of zero. How do you explain that?

            You can tweak the last snippet posted and instead of printing the merged arrays, nest the array_merge function inside the count function. If you really plan on learning PHP from scratch, you should be doing your research in a book or with a web tutorial, not a forum.


             8)
            The true sign of intelligence is not knowledge but imagination.

            -- Albert Einstein

            irshad

              Topic Starter


              Greenhorn

              • Experience: Beginner
              • OS: Windows 7
              Re: PHP Find Diffrence from 2 Arrays
              « Reply #13 on: August 23, 2014, 11:25:08 PM »
              thanks,
              i think i need to learn more about php, but please solve this problem at this time, i am thanks to a lot .. i am trying to learning php in behalf of your suggestion. thanks again.

              dear, i am trying to develop a account application, there is required me:
              1. produce product serial number
              2. sale produced serial number via sale invoice
              in this stage i am successful, but problem start here when product return
              3. when product return, product serial shown in database 2 time or 3 time
              4. and when return serial again sale when problem create

              result problem when created when serial number store double or triple or so on ... in the production table.

              please help me, on this stage, i shall learn more about php
              Irshad Siddique

              Sidewinder



                Guru

                Thanked: 139
              • Experience: Familiar
              • OS: Windows 10
              Re: PHP Find Diffrence from 2 Arrays
              « Reply #14 on: August 24, 2014, 02:40:00 PM »
              How did a question concerning two arrays with duplicate integer elements turn into an account application?

              An application is usually a major undertaking. At this point all I can make out is you have a data store of products and a data store of sales. That's not nearly enough to even point you in the right direction.

              Good luck.  8)

              Code: [Select]
              <?php

              //$array1 = array('1', '2', '2', '3', '3', '4', '5');
              //$array2 = array('1', '2', '3');

              //$array1 = array('1','2','3','4','5');
              //$array2 = array('3','4','5');

              $array1 = array(1,2,3,4,5);
              $array2 = array(1,2,3,4,5);
              $diff = array();

              foreach (
              $array1 as $item) {
                
              $key array_search($item$array2);
                if (
              $key !== False) {
                    unset(
              $array2[$key]);
                } else {
                    
              $diff[] = $item;
                }
              }

              print 
              "Number Of Differences: ".count(array_merge($diff$array2))."\n\r";
              foreach(
              array_merge($diff$array2) as $item) {
                print 
              $item." ";
              }
              ?>






              The true sign of intelligence is not knowledge but imagination.

              -- Albert Einstein