title: Create two equal height columns of random portrait and landscape images in PHP
body: <p><strong>Given a random number of portrait and landscape images, I am trying to write PHP code to output two columns of equal height:</strong></p>
  
  <ul>
  <li>By assigning the value "1" to landscape images and "2" to portraits, my idea is to divide the total images ($totalphotos) by two and then check to see that the image value ($photoval) doesn't exceed that number as it iterates through the loop.</li>
  <li>For example, if you have 6 total images and the first column already has 2 landscape images and the third image it encouters is a portrait, it will re-order the array (array_splice) and move the portrait image down and then contiue to the next image.</li>
  <li>If the output has already created two equal height columns (say 3 landscapes in the first column and 1 landscape + 1 portrait in the second column, the last image is dropped)</li>
  </ul>
  
  <p>I'm not sure if my approach is correct to try and output the html during the loop, or if it would make more sense to analyze the array and re-order the images first and then create a second loop to output the html.</p>
  
  <p>My code is a bit dirty and my "array_splice" method may not even be entirely correct. If you see a better way, feel free to scrap my code and show me something better! Any help is greatly appreciated!!</p>
  
  <pre><code>&lt;?php
  
       $photoval = 0;
       $totalcolumns = 0;
       $totalphotos = count($photos);
  
       for ($counter = 0; $counter &lt; $totalphotos; $counter++) :
  
              $photo = $photos[$counter];
  
              if ($photoval == 0) echo "        &lt;div class=\"column\"&gt;\n";
  
              if ($photo['orientation'] == "portrait" &amp;&amp; $photoval &gt;= $totalphotos/2) {
              if ($counter == $totalphotos)
                      echo "        &lt;/div&gt;\n";                
                      array_splice($photos, $counter, 1, array($photo));
  
                      continue;
              }
  ?&gt;
       &lt;div class="&lt;? echo $photo['orientation'] ?&gt; thumbnail"&gt;
            &lt;a href="&lt;?php echo $photo['link'] ?&gt;"&gt;
                 &lt;img src="&lt;?php if ($photo['orientation'] == "landscape") echo $photo['src']; else echo $photo['src_medium'];?&gt;" alt="&lt;? echo htmlentities($photo['caption'], ENT_QUOTES) ?&gt;"&gt;
            &lt;/a&gt;
       &lt;/div&gt;
  &lt;?php
              if ($photoval &gt;= $totalphotos/2 || $counter == $totalphotos) {
                  echo "        &lt;/div&gt;\n";
                  $photoval = 0;
                  $totalcolumns++;
                  if ($totalcolumns == 2)
                      break;
              }
  
       endfor;
  ?&gt;
  </code></pre>