Skipping items in a loop

From: Date: Wed, 18 Apr 2007 02:40:42 +0000
Subject: Skipping items in a loop
Groups: php.general 
Request: Send a blank email to php-general+get-253160@lists.php.net to get a copy of this message
Hello again; I have a directory that I am opening and reading to produce an array of contents; files and subdirectories. There are a number of subdirectories that I do not want to open and read the contents of, and others that I do want to open and read the contents of. This code is supposed to produce two arrays. One with directories and the other with files, including the path. This is so I can create the directories and copy the files. Certain files are specific to the source directory structure and I do not want them copied. The only way I can think of is to loop though the $dir_only array to see if it matches an item in the $dirs array so I can have the code by pass it. But that is in the middle of a while loop that is already in the middle of a loop. I have not been able to get it to work as I want. using php 5.1.2 Does anyone have a better suggestion (that will work in the context of this code, I do not want to do major rewriting if I can avoid it). Thanks in advance Jeff k The code follows:
                           $cont = array();
                           $dir_only = array();
                           $dir_only[0] = 'jk/collections';
                           $dir_only[1] = 'jk/extras';
                           $dir_only[2] = 'jk/group_info';
                           $dir_only[3] = 'jk/group_routes';
                           $dir_only[4] = 'jk/groups_in';
                           $dir_only[5] = 'jk/in';
                           $dir_only[6] = 'jk/log';
                           $dir_only[7] = 'jk/out';
                           $dir_only[8] = 'jk/requests';
                           $dir_only[9] = 'jk/store';
                          $dirs = array();
                          $dirs_a = array();
                          $files = array();
                          $cltag = '?'.'>'; // <- for php closing tag. If I do it literally, it screws up the syntax coloring in my code editor.
                          $rd = opendir('jk') or die("Could not open jk as source dir");
                          while($x = readdir($rd))
                               {
                                if($x == '..' || $x == '.')
                                   {
                                    continue;
                                    }
                                  else
                                   {
                                    array_push($cont, 'jk/'.$x);
                                   }
                                }
                           closedir($rd);
                           unset($rd);
                           unset($x);
                           for($i = 0; $i < count($cont); $i++)
                              {
                               if(is_dir($cont[$i]))
                                 {
                                  array_push($dirs, $cont[$i]);
                                 }
                               elseif(is_file($cont[$i]))
                                 {
                                  array_push($files, $cont[$i]);
                                 }
                               }
                           for($i = 0; $i < count($dirs); $i++) // looks at dirs
                              {
                                @$rd = opendir($dirs[$i]) or die("Could not open $dirs[$i]");
                                  while($x = readdir($rd))
                                       {
                                        if($x == '..' || $x == '.')
                                          {
                                            continue;
                                          }
                                         elseif(is_file($dirs[$i].'/'.$x))
                                           {
                                             array_push($files, $dirs[$i].'/'.$x);
                                           }
                                         elseif(is_dir($dirs[$i].'/'.$x) && $x != "")
                                           {
                                             array_push($dirs_a, $dirs[$i].'/'.$x);
                                           }
                                        }
                                  closedir($rd);
                                  unset($rd);
                                  unset($x);
                              };


« previous php.general (#253160) next »