Comparing contents of two huge arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sarega
    New Member
    • Oct 2008
    • 82

    Comparing contents of two huge arrays

    Hi,
    I have two very huge arrays, the second array contains some of the elements of array1 and also different elements, both are not of the same size, have to find only the elements that are not common in both the arrays, I used array_diff but its not working can some body help me with this??

    Thanks in advance
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Post the code you have tried. Use code tags.

    Comment

    • sarega
      New Member
      • Oct 2008
      • 82

      #3
      Code:
      <?
      $array1=array("++Hello how are you      20000901",
      "++Hows work     20010201",
      "++Am doing great  30089254");
      $array2=array("-+Hows work     20010201",
      "-+I like PHP    2657656",
      "-+Am doing great    30089254");
      $arry3=array_diff($array1,$array2);
      print_r($arry3);
      ?>
      the output should display just first element of array1 i.e ++Hello how are you 20000901
      here am just giving a part of the input the actual input has more elements of similar format.

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        The array elements all differ
        [PHP]"++Hello how are you 20000901",
        "++Hows work 20010201",
        "++Am doing great 30089254");

        "-+Hows work 20010201",
        "-+I like PHP 2657656",
        "-+Am doing great 30089254");[/PHP]

        Comment

        • sarega
          New Member
          • Oct 2008
          • 82

          #5
          Actually I have remove that -+ and ++ and then compare but how do I remove that??

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            but how do I remove -+ and ++
            You will have to use string functions to remove.
            str_replace is a simple method [PHP]$remove = array('++','-+');
            str_replace($re move,'',$array) ;[/PHP]However this means looping through every array element before doing the array_diff.
            You could use one of the array functions that employ a callback function
            or array_walk().

            Is it not better to strip the operator symbols off the string before inserting in the array?

            Comment

            • sarega
              New Member
              • Oct 2008
              • 82

              #7
              It is not possible to remove the -+ and ++ before inserting into the array. But how do I use the array_walk?is it possible the function in array_walk to return a value??

              Comment

              • sarega
                New Member
                • Oct 2008
                • 82

                #8
                Can I create a singly linked list for comparing the arrays?If yes how to do it??

                Comment

                • sarega
                  New Member
                  • Oct 2008
                  • 82

                  #9
                  I used the following code to display only non duplicate elements but am not getting the required results can you plz tell me the problem with this code here??[code=php]$rep=array("-+");
                  foreach($array1 as $array) {
                  $x=str_replace( $rep,"",$array) ;
                  array_push($a,$ array);
                  }
                  $repl=array("++ ");
                  foreach($array2 as $array) {
                  $y=str_replace( $repl,"",$array );
                  array_push($b,$ array);
                  }
                  $c=array_merge( $a,$b);
                  $d=array_unique ($c);[/code]
                  Last edited by pbmods; Nov 9 '08, 02:31 PM. Reason: Added CODE tags.

                  Comment

                  Working...