Connecting Tech Pros Worldwide Help | Site Map

Comparing contents of two huge arrays

  #1  
Old November 6th, 2008, 02:00 PM
Member
 
Join Date: Oct 2008
Posts: 82
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
  #2  
Old November 6th, 2008, 02:22 PM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,862
Provided Answers: 9

re: Comparing contents of two huge arrays


Post the code you have tried. Use code tags.
  #3  
Old November 6th, 2008, 02:33 PM
Member
 
Join Date: Oct 2008
Posts: 82

re: Comparing contents of two huge arrays


Expand|Select|Wrap|Line Numbers
  1. <?
  2. $array1=array("++Hello how are you      20000901",
  3. "++Hows work     20010201",
  4. "++Am doing great  30089254");
  5. $array2=array("-+Hows work     20010201",
  6. "-+I like PHP    2657656",
  7. "-+Am doing great    30089254");
  8. $arry3=array_diff($array1,$array2);
  9. print_r($arry3);
  10. ?>
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.
  #4  
Old November 6th, 2008, 03:28 PM
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,064
Provided Answers: 2

re: Comparing contents of two huge arrays


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]
  #5  
Old November 7th, 2008, 04:03 AM
Member
 
Join Date: Oct 2008
Posts: 82

re: Comparing contents of two huge arrays


Actually I have remove that -+ and ++ and then compare but how do I remove that??
  #6  
Old November 7th, 2008, 10:36 AM
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,064
Provided Answers: 2

re: Comparing contents of two huge arrays


Quote:
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($remove,'',$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?
  #7  
Old November 8th, 2008, 04:51 PM
Member
 
Join Date: Oct 2008
Posts: 82

re: Comparing contents of two huge arrays


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??
  #8  
Old November 8th, 2008, 05:44 PM
Member
 
Join Date: Oct 2008
Posts: 82

re: Comparing contents of two huge arrays


Can I create a singly linked list for comparing the arrays?If yes how to do it??
  #9  
Old November 9th, 2008, 01:47 PM
Member
 
Join Date: Oct 2008
Posts: 82

re: Comparing contents of two huge arrays


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??
Expand|Select|Wrap|Line Numbers
  1. $rep=array("-+");
  2. foreach($array1 as $array) {
  3. $x=str_replace($rep,"",$array);
  4. array_push($a,$array);
  5. }
  6. $repl=array("++");
  7. foreach($array2 as $array) {
  8. $y=str_replace($repl,"",$array);
  9. array_push($b,$array);
  10. }
  11. $c=array_merge($a,$b);
  12. $d=array_unique($c);

Last edited by pbmods; November 9th, 2008 at 02:31 PM. Reason: Added CODE tags.
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 08:57 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 07:46 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 03:55 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 5 November 14th, 2005 12:36 PM