Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/150177-solved-compare-arrays/
Share on other sites

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 />";
}
}


?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.