homer.favenir Posted June 4, 2008 Share Posted June 4, 2008 hi to all, array_diff compare 2 arrays and returns a line that is different. i need to reverse it, i need all the lines that matches with both arrays. what is the function for that guys? thanks Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 4, 2008 Share Posted June 4, 2008 array_intersect()? Looking in the manual at the function you know about (e.g. http://www.php.net/array_diff) will help you find similar functions by looking under the section 'See Also'. Quote Link to comment Share on other sites More sharing options...
beboo002 Posted June 4, 2008 Share Posted June 4, 2008 use this for reverse array <?php $a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse"); print_r(array_reverse($a)); ?> Quote Link to comment Share on other sites More sharing options...
homer.favenir Posted June 4, 2008 Author Share Posted June 4, 2008 array_intersect()? Looking in the manual at the function you know about (e.g. http://www.php.net/array_diff) will help you find similar functions by looking under the section 'See Also'. thanks, it works, but the it didnt extract the last line. e.g. only 3 lines are the same on both array1 and array2, only 2 lines were return and the last line isnt.... any advice... thanks! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 4, 2008 Share Posted June 4, 2008 How about posting some sample code? Quote Link to comment Share on other sites More sharing options...
homer.favenir Posted June 4, 2008 Author Share Posted June 4, 2008 function intersect($str1, $str2) { $count = 0; $dir = 'C:\Documents and Settings\All Users\Desktop\portfoliocompare\\'; $file = $_POST['newFile']; $file = $dir . $file . ".txt"; if(file_exists($file)) {?> <table border=0> <tr> <td> <h2><font color="#FF0000">Warning! </font>File Already Exist. Pls Rename</h2> </td> </tr> </table> <?php }else{ $file = trim($file); $file = fopen($file, 'w'); $diff = array_intersect($str1, $str2); foreach($diff as $key => $line) { echo $line . "<br>"; fwrite($file, $line); $count++; } echo $count . " " . "Records found"; } } Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 4, 2008 Share Posted June 4, 2008 How about narrowing down the problem? I've no idea what the above code does and i've also no idea what your previous discription meant. How about providing sample arrays of $str1, $str2 and what iis in $diff. Im assuming that's where the problem lies? Quote Link to comment Share on other sites More sharing options...
homer.favenir Posted June 4, 2008 Author Share Posted June 4, 2008 I already know the cause, the first txt file to compare has extra last line (but no characters), the second txt file to compare has no extra last line. that is why it didnt extracted the last line. how to ignore if the txt file has an extra last line or it hasnt? thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.