Issue merging DirectoryIterator arrays in PHP
<p>I'm having an an issue trying to merge a few arrays together for a project I'm working on. I'm getting closer to the desired results and I've searched this site and can't find a working solution.</p>

<p>I doubt it matters, but this project is being tested on IIS but the end result has to work on both IIS and Apache.</p>

<p>So here goes... I have four directories that I need to merge and sort alphabetical.</p>

<pre><code>$dir1 = "D:\\dir1";
$dir2 = "D:\\dir2";
$dir3 = "D:\\dir3";
$dir4 = "D:\\dir4";
$dirA = array($dir1,$dir2,$dir3,$dir4);
</code></pre>

<p>If I do the following I'm getting 'close' to the results I need:</p>

<pre><code>try
{
  $object = new ArrayIterator($dirA);
  foreach($object as $key=&gt;$value)
  {
    foreach(new DirectoryIterator($value) as $item)
    {
      if(!$item-&gt;isDot())
      {
        echo $item-&gt;getFilename() .' ';
        if(file_exists($value.'\\'.$item.'\folder.jpg'))
        {
          echo 'Y&lt;br /&gt;';
        } else {
          echo 'X&lt;br /&gt;';
        }
      }
    }
  }
}
catch(Exception $e)
{
  echo $e-&gt;getMessage();
}
</code></pre>

<p>and end up with a list of folders from each directory (in alphabetical order) but they don't merge, instead it still groups them by <code>$item-&gt;key()</code> (which I guess is what I'm actually trying to merge? I have tried numerous ways of using <code>array_merge</code> with no success)</p>

<p>I've tried using <code>sort</code> and <code>ksort</code>, etc. but either they wont work or I can't figure out the correct placement of it to work before I <code>echo</code> the list.</p>

<p>My end goal is to <code>echo</code> this entire thing into a table where I use multiple <code>file_exists</code> to check for specific files that 'should' be available in every one of the folders... for example, each should have a 'folder.jpg' in it's root, so I'd want it to show something like:</p>

<pre><code>Folder Name            folder.jpg      (other files to be verify)
Directory1                 Y            Y   Y   Y   Y   Y
Directory2                 Y            Y   X   Y   Y   Y
Directory3                 X            X   Y   X   X   X
Directory4                 Y            Y   Y   Y   Y   Y
</code></pre>

<p>--<br>
Hopefully the above isn't confusing and provides enough information, I want to thank anyone who is able to help and offer an invite to anyone who would be willing to help out with the project to contact me (I'll obviously credit the person on the corresponding areas).</p>

<p>Thanks again.</p>

<p>--<br>
Edit: Trying to clarify a bit.</p>

<p>In the end, this project will write certain variables to a 'settings.ini' and read those (I have that part working fine, so I'm ignoring that altogether right now and writing everything locally in a single file).</p>

<p>In my case, I have 4 folders on different drives, each folder has between 15 and 130 sub-directories (all unique names). So an abbreviated list would look similar to:</p>

<pre><code>T:\New Set\
     A\
     D\
     G\
D:\Old Set\
     B\
     F\
  etc.

---------
$dir1 = 'T:/New Set';
$dir2 = 'D:/Old Set';
$dir3 = 'D:/Overage';
$dir4 = 'G:/Unsorted';
$dirA = array($dir1,$dir2,$dir3,$dir4);
</code></pre>

<p>Using the code I originally posted above would give me something like below (instead of them alphabetical)</p>

<pre><code>A  // from $dir1
D  // from $dir1
G  // from $dir1
B  // from $dir2
F  // from $dir2
etc.
</code></pre>

<p>Using the code posted by DaveRandom, it displayed the following:</p>

<pre><code>Array ( [T:\New Set] =&gt; Array ( [folder.jpg] =&gt; 1 [cover.jpg] =&gt; [poster.jpg] =&gt; ) [D:\Old Set] =&gt; Array ( [folder.jpg] =&gt; 1 [cover.jpg] =&gt; [poster.jpg] =&gt; ) [D:\Overage] =&gt; Array ( [folder.jpg] =&gt; 1 [cover.jpg] =&gt; [poster.jpg] =&gt; ) [G:\Unsorted] =&gt; Array ( [folder.jpg] =&gt; 1 [cover.jpg] =&gt; [poster.jpg] =&gt; ) )
Folder Name folder.jpg  cover.jpg   poster.jpg
T:\New Set  Y   X   X
D:\Old Set  Y   X   X
D:\Overage  Y   X   X
G:\Unsorted Y   X   X
</code></pre>

<p>I guess for now, the only question I have is solely sorting the sub-directories since I'm able to verify the files and do the table in other ways. So to make it clear, I want the directory array to not display like above, but like the below instead:</p>

<pre><code>A  // from $dir1
B  // from $dir2
D  // from $dir1
F  // from $dir2
G  // from $dir1
etc.
</code></pre>
