Loading ...
Sorry, an error occurred while loading the content.

Compare two array and return difference from both array

Expand Messages
  • Shiva Kumar Mallikarjun
    Hi All, i ve two array, compare two array and return difference from both array keys only.. $array1 = array( a = greenss , b = brown , c =
    Message 1 of 6 , Apr 3 8:33 AM
    • 0 Attachment
      Hi All,

      i've two array, compare two array and return difference from both array keys only..


      $array1 = array("a" => "greenss", "b" => "brown", "c" =>
      "asdfsad","e" => "asdfsad","p" => "asdfsad");
      $array2 = array("a" => "green", "b" => "brown", "c"
      =>"shiva","d" =>"red","e" => "asdfsad");


      it should return

      array("a", "c", "d","p") .... list of different keys according to value.


      Thanks,

      Shiva
      ________________________________


      The INTERNET now has a personality. YOURS! See your Yahoo! Homepage. http://in.yahoo.com/

      [Non-text portions of this message have been removed]
    • Minesh Shah
      Hello, here is a solution for you. $a1 = array( a = greenss , b = brown , c = asdfsad , e = asdfsad , p = asdfsad ); $a2 = array( a =
      Message 2 of 6 , Apr 3 1:34 PM
      • 0 Attachment
        Hello,

        here is a solution for you.

        $a1 = array("a" => "greenss", "b" => "brown", "c" =>"asdfsad","e" =>
        "asdfsad","p" => "asdfsad");
        $a2 = array("a" => "greenss", "b" => "browns", "c" =>"shiva","d" =>"red","e"
        => "asdfsad");
        $res_a= array_keys($a1);
        $res_b= array_keys($a2);
        $result= array_unique(array_merge($res_a,$res_b));
        foreach($result as $k=>$v):
        if($a1[$v]==$a2[$v])
        continue;
        $status[]=$v;
        if(isset($a1[$v]))
        unset($a1[$v]);
        if(isset($a2[$v]))
        unset($a2[$v]);

        endforeach;

        echo "<pre>";
        print_r($status);


        On Sat, Apr 3, 2010 at 9:03 PM, Shiva Kumar Mallikarjun <
        shivakumar_rnm@...> wrote:

        >
        >
        > Hi All,
        >
        > i've two array, compare two array and return difference from both array
        > keys only..
        >
        > $array1 = array("a" => "greenss", "b" => "brown", "c" =>
        > "asdfsad","e" => "asdfsad","p" => "asdfsad");
        > $array2 = array("a" => "green", "b" => "brown", "c"
        > =>"shiva","d" =>"red","e" => "asdfsad");
        >
        > it should return
        >
        > array("a", "c", "d","p") .... list of different keys according to value.
        >
        > Thanks,
        >
        > Shiva
        > ________________________________
        >
        > The INTERNET now has a personality. YOURS! See your Yahoo! Homepage.
        > http://in.yahoo.com/
        >
        > [Non-text portions of this message have been removed]
        >
        >
        >


        [Non-text portions of this message have been removed]
      • SIVA KUMAR
        Shiva,   array_diff function is helpful for your requirement. And then using array_keys values you can get kery values.   Thanks Sivakumar.B ... From: Shiva
        Message 3 of 6 , Apr 4 8:59 AM
        • 0 Attachment
          Shiva,
           
          array_diff function is helpful for your requirement. And then using array_keys values you can get kery values.
           
          Thanks
          Sivakumar.B

          --- On Sat, 4/3/10, Shiva Kumar Mallikarjun <shivakumar_rnm@...> wrote:


          From: Shiva Kumar Mallikarjun <shivakumar_rnm@...>
          Subject: [bang-phpug] Compare two array and return difference from both array
          To: bang-phpug@yahoogroups.com
          Date: Saturday, April 3, 2010, 9:03 PM


           



          Hi All,

          i've two array, compare two array and return difference from both array keys only..

          $array1 = array("a" => "greenss", "b" => "brown", "c" =>
          "asdfsad","e" => "asdfsad","p" => "asdfsad");
          $array2 = array("a" => "green", "b" => "brown", "c"
          =>"shiva","d" =>"red","e" => "asdfsad");

          it should return

          array("a", "c", "d","p") .... list of different keys according to value.

          Thanks,

          Shiva
          ____________ _________ _________ __

          The INTERNET now has a personality. YOURS! See your Yahoo! Homepage. http://in.yahoo com/

          [Non-text portions of this message have been removed]











          [Non-text portions of this message have been removed]
        • Shiva Kumar Mallikarjun
          Thanks Minesh Shah, your solution works perfectly for me. array_keys , array_diff and array_diff_assoc doesn t satisfy my requirement
          Message 4 of 6 , Apr 6 12:32 AM
          • 0 Attachment
            Thanks Minesh Shah, your solution works perfectly for me.

            array_keys , array_diff and array_diff_assoc doesn't satisfy my requirement






            ________________________________
            From: Minesh Shah <shahmineshr@...>
            To: bang-phpug@yahoogroups.com
            Sent: Sun, 4 April, 2010 2:04:08 AM
            Subject: Re: [bang-phpug] Compare two array and return difference from both array


            Hello,

            here is a solution for you.

            $a1 = array("a" => "greenss", "b" => "brown", "c" =>"asdfsad", "e" =>
            "asdfsad","p" => "asdfsad");
            $a2 = array("a" => "greenss", "b" => "browns", "c" =>"shiva","d" =>"red","e"
            => "asdfsad");
            $res_a= array_keys($ a1);
            $res_b= array_keys($ a2);
            $result= array_unique( array_merge( $res_a,$res_ b));
            foreach($result as $k=>$v):
            if($a1[$v]== $a2[$v])
            continue;
            $status[]=$v;
            if(isset($a1[ $v]))
            unset($a1[$v] );
            if(isset($a2[ $v]))
            unset($a2[$v] );

            endforeach;

            echo "<pre>";
            print_r($status) ;

            On Sat, Apr 3, 2010 at 9:03 PM, Shiva Kumar Mallikarjun <
            shivakumar_rnm@ yahoo.com> wrote:

            >
            >
            > Hi All,
            >
            > i've two array, compare two array and return difference from both array
            > keys only..
            >
            > $array1 = array("a" => "greenss", "b" => "brown", "c" =>
            > "asdfsad","e" => "asdfsad","p" => "asdfsad");
            > $array2 = array("a" => "green", "b" => "brown", "c"
            > =>"shiva","d" =>"red","e" => "asdfsad");
            >
            > it should return
            >
            > array("a", "c", "d","p") .... list of different keys according to value.
            >
            > Thanks,
            >
            > Shiva
            > ____________ _________ _________ __
            >
            > The INTERNET now has a personality. YOURS! See your Yahoo! Homepage.
            > http://in.yahoo com/
            >
            > [Non-text portions of this message have been removed]
            >
            >
            >

            [Non-text portions of this message have been removed]





            Your Mail works best with the New Yahoo Optimized IE8. Get it NOW! http://downloads.yahoo.com/in/internetexplorer/

            [Non-text portions of this message have been removed]
          • Minesh Shah
            Hello Shiva, Thanks for your complements. If you need any kind of help regarding PHP then let me know anytime. As i am mastering in CakePHP and MVC based
            Message 5 of 6 , Apr 6 8:42 AM
            • 0 Attachment
              Hello Shiva,

              Thanks for your complements. If you need any kind of help regarding PHP then
              let me know anytime.

              As i am mastering in CakePHP and MVC based architectures, you can ask and
              refer any body, as i'm ready to help anybody for 24 * 7.

              Thanks,
              Minesh Shah

              On Tue, Apr 6, 2010 at 1:02 PM, Shiva Kumar Mallikarjun <
              shivakumar_rnm@...> wrote:

              >
              >
              > Thanks Minesh Shah, your solution works perfectly for me.
              >
              > array_keys , array_diff and array_diff_assoc doesn't satisfy my requirement
              >
              >
              > ________________________________
              > From: Minesh Shah <shahmineshr@... <shahmineshr%40gmail.com>>
              >
              > To: bang-phpug@yahoogroups.com <bang-phpug%40yahoogroups.com>
              > Sent: Sun, 4 April, 2010 2:04:08 AM
              > Subject: Re: [bang-phpug] Compare two array and return difference from both
              > array
              >
              >
              > Hello,
              >
              > here is a solution for you.
              >
              > $a1 = array("a" => "greenss", "b" => "brown", "c" =>"asdfsad", "e" =>
              > "asdfsad","p" => "asdfsad");
              > $a2 = array("a" => "greenss", "b" => "browns", "c" =>"shiva","d"
              > =>"red","e"
              > => "asdfsad");
              > $res_a= array_keys($ a1);
              > $res_b= array_keys($ a2);
              > $result= array_unique( array_merge( $res_a,$res_ b));
              > foreach($result as $k=>$v):
              > if($a1[$v]== $a2[$v])
              > continue;
              > $status[]=$v;
              > if(isset($a1[ $v]))
              > unset($a1[$v] );
              > if(isset($a2[ $v]))
              > unset($a2[$v] );
              >
              > endforeach;
              >
              > echo "<pre>";
              > print_r($status) ;
              >
              > On Sat, Apr 3, 2010 at 9:03 PM, Shiva Kumar Mallikarjun <
              > shivakumar_rnm@ yahoo.com> wrote:
              >
              > >
              > >
              > > Hi All,
              > >
              > > i've two array, compare two array and return difference from both array
              > > keys only..
              > >
              > > $array1 = array("a" => "greenss", "b" => "brown", "c" =>
              > > "asdfsad","e" => "asdfsad","p" => "asdfsad");
              > > $array2 = array("a" => "green", "b" => "brown", "c"
              > > =>"shiva","d" =>"red","e" => "asdfsad");
              > >
              > > it should return
              > >
              > > array("a", "c", "d","p") .... list of different keys according to value.
              > >
              > > Thanks,
              > >
              > > Shiva
              > > ____________ _________ _________ __
              > >
              > > The INTERNET now has a personality. YOURS! See your Yahoo! Homepage.
              > > http://in.yahoo com/
              > >
              > > [Non-text portions of this message have been removed]
              > >
              > >
              > >
              >
              > [Non-text portions of this message have been removed]
              >
              > Your Mail works best with the New Yahoo Optimized IE8. Get it NOW!
              > http://downloads.yahoo.com/in/internetexplorer/
              >
              >
              > [Non-text portions of this message have been removed]
              >
              >
              >


              [Non-text portions of this message have been removed]
            • Ved Prakash
              Hi Shiva, check this out. greenss , b = brown , c = asdfsad , e = asdfsad , p = asdfsad ); $array2 = array( a =
              Message 6 of 6 , Apr 6 3:00 PM
              • 0 Attachment
                Hi Shiva,

                check this out.

                <?php
                $array1 = array("a" => "greenss", "b" => "brown", "c" => "asdfsad","e" =>
                "asdfsad","p" => "asdfsad");
                $array2 = array("a" => "green", "b" => "brown", "c"=>"shiva","d"
                =>"red","e" => "asdfsad");
                $resarr = array_keys(array_diff_assoc($array1, $array2));
                $resarr2 = array_keys(array_diff_assoc($array2, $array1));
                $narr = array_unique(array_merge($resarr,$resarr2));
                print_r($narr);
                ?>

                - Ved


                [Non-text portions of this message have been removed]
              Your message has been successfully submitted and would be delivered to recipients shortly.