Dustin013 Posted March 19, 2009 Share Posted March 19, 2009 I have a function that is returning the directory names from a given path. These directory names are placed into an array. I would like to call the function twice and compare the contents of the array... (The below code works, just wanted to give an example of what I am trying to do) function returnDirectory($dirname) { $ignore= array('.', '..', '.cpan', '.cpcpan', 'MySQL-install'); $files = array(); if($handle = opendir($dirname)) { while(false !== ($file = readdir($handle))){ if(!in_array($file, $ignore)){ $files[] = $file; } } closedir($handle); } return($files); // this will return an array of files } // List files in home directory $filearr = returnDirectory('/home'); sort($filearr); foreach($filearr as $file){ echo $file."<br />"; } // End listing files for home directory // If I want to also scan another directory using the same function // List files in svn directory $filearr2 = returnDirectory('/svn'); sort($filearr2); foreach($filearr2 as $file){ echo $file."<br />"; } // End listing files for svn directory Basically, what I would like to do, is compare the two arrays. What I am trying to do is make two listings, one that outputs directories that "ARE IN BOTH" the /home directory and the /svn directory. The other listing would show the directories that are in the home directory, but "NOT" in the SVN directory. So all and all, I guess I just need to figure out the best way to compare the arrays. I have looked at the array_search function, which I would technically use but it seems rather inefficient. Any suggestions are greatly appreciated! Any suggestions on how to get this accomplished? Quote Link to comment https://forums.phpfreaks.com/topic/150177-solved-compare-arrays/ Share on other sites More sharing options...
sloth456 Posted March 19, 2009 Share Posted March 19, 2009 I'm afraid I haven't taken the time to look over your code in detail, but would in_array() be what you're looking for? Quote Link to comment https://forums.phpfreaks.com/topic/150177-solved-compare-arrays/#findComment-788663 Share on other sites More sharing options...
sasa Posted March 19, 2009 Share Posted March 19, 2009 look functions array_diff() array_intersect() Quote Link to comment https://forums.phpfreaks.com/topic/150177-solved-compare-arrays/#findComment-788666 Share on other sites More sharing options...
Dustin013 Posted March 19, 2009 Author Share Posted March 19, 2009 Actually, went outside and smoked a cig and gave it some thought... here is what I have done and it worked ;-) <?php // Set paths to home and SVN $homedir = "/home"; $svndir = "/svn"; function returnDirectory($dirname) { $ignore= array('.', '..'); $files = array(); if($handle = opendir($dirname)) { while(false !== ($file = readdir($handle))){ if(!in_array($file, $ignore)){ $files[] = $file; } } closedir($handle); } return($files); // this will return an array of files } // Build array for home directory $homearr = returnDirectory($homedir); sort($homearr); // Build array for svn directory $svnarr = returnDirectory($svndir); sort($svnarr); // Output files that are IN the home directory and NOT in the SVN directory echo '<br /><b>Projects currently NOT in SVN</b><br />'; foreach($homearr as $file){ if(!in_array($file, $svnarr)){ $files[] = $file; echo $file."<br />"; } } // Output the names of directories that are in BOTH the HOME directory and SVN directory echo '<br /><b>Projects currently in SVN</b><br />'; foreach($homearr as $file){ if(in_array($file, $svnarr)){ $files[] = $file; echo $file."<br />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150177-solved-compare-arrays/#findComment-788677 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.