Web Analytics Made Easy -
StatCounter Array_Diff - CodingForum

Announcement

Collapse
No announcement yet.

Array_Diff

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Array_Diff

    I have two arrays I have been trying to compare for a month with no luck. I want to know the names in $arr1 that are not in $arr2.

    Both arrays are results from mysql queries. I have managed to get them to look the same ie:

    $arr1=
    PHP Code:
    Array
    (
        [
    1] => Aangel
        
    [2] => Aarowyn
        
    [3] => Abhram
        
    [4] => Anonymous
        
    [5] => Alfield
        
    [6] => Alomar
        
    [7] => Aloohell
        
    [8] => Carrya
        
    [9] => Ardriella
        
    [10] => Asavenge 
    and $arr2 =
    PHP Code:
    Array
    (
        [
    1] => Anonymous
        
    [2] => Carrya
        
    [3] => AdsBot [Google]
        [
    4] => Alexa [Bot]
        [
    5] => Alta Vista [Bot]
        [
    6] => Ask Jeeves [Bot]
        [
    7] => Baidu [Spider]
        [
    8] => Exabot [Bot]
        [
    9] => FAST Enterprise [Crawler]
        [
    10] => FAST WebCrawler [Crawler
    however when I do
    PHP Code:
    $arr array_diff($arr1$arr2); 
    I receive an array with the same contents as $arr1. No matter how I change things around it doesn't work. What am I missing here?

  • #2
    How about:

    PHP Code:
    foreach ($arr1 as $key=>$value){
     if (!
    in_array($value,$arr2)){
    echo 
    $value.' not in array2<br />';
    }

    See if that works. I think it should but have not tested. You may also need to lowercase all the values just to make sure.

    You may also want to try soundex() to see if some values are similar.

    D.
    Daemonkin.
    If this was helpful, please add to my reputation
    Thousand Sons - Freelance Web Developer - ninetyonedegrees.com

    Comment


    • #3
      I ran the following:
      PHP Code:
      $arr1 = array('Aangel''Aarowyn''Abhram''Anonymous''Alfield''Alomar''Aloohell''Aloohell''Carrya''Ardriella''Asavenge');
          
      $arr2 = array('Anonymous''Carrya''AdsBot [Google]''Alexa [Bot]''Alta Vista [Bot]''Ask Jeeves [Bot]''Baidu [Spider]''Exabot [Bot]''FAST Enterprise [Crawler]''FAST WebCrawler [Crawler]');
          foreach (
      $arr1 as $key => $value) {
            if (!
      in_array($value$arr2)) {
              
      $arr[] = $value ;
            }
          } 
      And got the output:

      Code:
      Array
      (
          [0] => Aangel
          [1] => Aarowyn
          [2] => Abhram
          [3] => Alfield
          [4] => Alomar
          [5] => Aloohell
          [6] => Aloohell
          [7] => Ardriella
          [8] => Asavenge
      )
      D
      Daemonkin.
      If this was helpful, please add to my reputation
      Thousand Sons - Freelance Web Developer - ninetyonedegrees.com

      Comment


      • #4
        I receive an array with the same contents as $arr1. No matter how I change things around it doesn't work. What am I missing here?
        Are you sure?
        PHP Code:
        $arr1 = array('Aangel''Aarowyn''Abhram''Anonymous''Alfield''Alomar''Aloohell''Aloohell''Carrya''Ardriella''Asavenge');
        $arr2 = array('Anonymous''Carrya''AdsBot [Google]''Alexa [Bot]''Alta Vista [Bot]''Ask Jeeves [Bot]''Baidu [Spider]''Exabot [Bot]''FAST Enterprise [Crawler]''FAST WebCrawler [Crawler]');
        $result array_diff($arr1$arr2); 

        myPrintr($arr1); 
         
        myPrintr($arr2); 
         
        myPrintr($result); 
        function 
        myPrintr($arr){  
          echo 
        '<pre>';
          
        print_r($arr);
          echo 
        '</pre>';

        Digitalocean Cloud Hosting (Referral link - get $10 free credit) Fameco

        Comment


        • #5
          add a $tmp array and store values that have already passed your check and recompare your check with values that have passed not to be in array because your arr1 contains duplicate keys.

          PHP Code:
          <?php
          $arr1 
          = array('Aangel''Aarowyn''Abhram''Anonymous''Alfield''Alomar''Aloohell''Aloohell''Carrya''Ardriella''Asavenge');
              
          $arr2 = array('Anonymous''Carrya''AdsBot [Google]''Alexa [Bot]''Alta Vista [Bot]''Ask Jeeves [Bot]''Baidu [Spider]''Exabot [Bot]''FAST Enterprise [Crawler]''FAST WebCrawler [Crawler]');
              
          $tmp = array();
              foreach (
          $arr1 as $key => $value) {
                if (!
          in_array($value$arr2) && !in_array($value$tmp)) {
                  
          $tmp[] = $value ;
                  
          $arr[] = $value ;
                }
              } 
              print 
          "<pre>";
              
          print_r($arr);
          ?>
          You can also use some PHP function to remove duplicate array entries in $arr1 and then compare it with arr2 (i know that function exists but i don't know how's it called and don't have time to look for it).
          PHP Idea Factory

          Comment


          • #6
            Funnymoney: I think that was my fault in duplicating the data in $arr1. the OP had only single instance data.

            D.
            Daemonkin.
            If this was helpful, please add to my reputation
            Thousand Sons - Freelance Web Developer - ninetyonedegrees.com

            Comment


            • #7
              wait, i don't get it, then it works?
              usually when ppl solve their problems they mention it, but you didn't write that you solved it, just posted output array that kinda had a duplicate..

              dunno now, did you manage to solve it or not

              anyway all solutions posted here give result you wanted. maybe there's something else you need, i just can't figure it out
              PHP Idea Factory

              Comment


              • #8
                1. I gave a solution not using array_diff
                2. I am not original poster.
                3. my head hurts now


                D.
                Daemonkin.
                If this was helpful, please add to my reputation
                Thousand Sons - Freelance Web Developer - ninetyonedegrees.com

                Comment


                • #9
                  Originally posted by daemonkin View Post
                  • I am not original poster.


                  D.
                  damn, didn't even notice that.. sorry
                  PHP Idea Factory

                  Comment


                  • #10
                    Originally posted by Hayyel View Post
                    I have two arrays I have been trying to compare for a month with no luck. I want to know the names in $arr1 that are not in $arr2.

                    Both arrays are results from mysql queries. I have managed to get them to look the same ie:

                    $arr1=
                    PHP Code:
                    Array
                    (
                        [
                    1] => Aangel
                        
                    [2] => Aarowyn
                        
                    [3] => Abhram
                        
                    [4] => Anonymous
                        
                    [5] => Alfield
                        
                    [6] => Alomar
                        
                    [7] => Aloohell
                        
                    [8] => Carrya
                        
                    [9] => Ardriella
                        
                    [10] => Asavenge 
                    and $arr2 =
                    PHP Code:
                    Array
                    (
                        [
                    1] => Anonymous
                        
                    [2] => Carrya
                        
                    [3] => AdsBot [Google]
                        [
                    4] => Alexa [Bot]
                        [
                    5] => Alta Vista [Bot]
                        [
                    6] => Ask Jeeves [Bot]
                        [
                    7] => Baidu [Spider]
                        [
                    8] => Exabot [Bot]
                        [
                    9] => FAST Enterprise [Crawler]
                        [
                    10] => FAST WebCrawler [Crawler
                    however when I do
                    PHP Code:
                    $arr array_diff($arr1$arr2); 
                    I receive an array with the same contents as $arr1. No matter how I change things around it doesn't work. What am I missing here?
                    None of the names in $arr1 are in $arr2 .... so the result you got is correct, what were you expecting?
                    http://www.hazelryan.co.uk

                    Comment


                    • #11
                      Originally posted by NancyJ
                      None of the names in $arr1 are in $arr2 .... so the result you got is correct, what were you expecting?
                      My above posted code gives
                      Code:
                      Array
                      (
                          [0] => Aangel
                          [1] => Aarowyn
                          [2] => Abhram
                          [3] => [ICODE]Anonymous[/ICODE]
                          [4] => Alfield
                          [5] => Alomar
                          [6] => Aloohell
                          [7] => Aloohell
                          [8] => [ICODE]Carrya[/ICODE]
                          [9] => Ardriella
                          [10] => Asavenge
                      )
                      
                      Array
                      (
                          [0] => [ICODE]Anonymous[/ICODE]
                          [1] => [ICODE]Carrya[/ICODE]
                          [2] => AdsBot [Google]
                          [3] => Alexa [Bot]
                          [4] => Alta Vista [Bot]
                          [5] => Ask Jeeves [Bot]
                          [6] => Baidu [Spider]
                          [7] => Exabot [Bot]
                          [8] => FAST Enterprise [Crawler]
                          [9] => FAST WebCrawler [Crawler]
                      )
                      
                      Array
                      (
                          [0] => Aangel
                          [1] => Aarowyn
                          [ICODE][2][/ICODE] => Abhram
                          [ICODE][4][/ICODE] => Alfield
                          [5] => Alomar
                          [6] => Aloohell
                          [ICODE][7][/ICODE] => Aloohell
                          [ICODE][9][/ICODE] => Ardriella
                          [10] => Asavenge
                      )
                      . Right?
                      Last edited by abduraooft; Apr 2, 2009, 11:52 AM.
                      Digitalocean Cloud Hosting (Referral link - get $10 free credit) Fameco

                      Comment


                      • #12
                        Ah, I missed those - at first glance I thought the list was alphabetical. Anyway, arary_diff($arr1,$arr2); works perfectly on the data presented in the OP. If entries aren't being removed that appear to be in $arr2 then its probably case sensitivity or white space
                        http://www.hazelryan.co.uk

                        Comment


                        • #13
                          So if the example here works it must be something in the array? I copied and pasted the array data as given by print_r.

                          THose were not all of the values as each array contains thousands.

                          How could I rebuild the arrays to ensure data is what is expected?

                          Comment


                          • #14
                            Who knows, maybe you should buy PHP 5.3 or latter
                            PHP Idea Factory

                            Comment


                            • #15
                              What would the significance of the upgrade from 5.2.9 to 5.3 or later be? Its not my server so thats out anyway.

                              Comment

                              Working...
                              X