title: Wordpress: How to implement a for-each loop?
body: <p>I am trying to get all the images attached to a post. According to the <a href="http://www.rlmseo.com/blog/get-images-attached-to-post/" rel="nofollow">original author of this code</a>, this can be achieved by using a "for-each loop to loop through each of the values in $arrKeys". Does anyone know how to implement this?</p>
  
  <p><strong>PHP</strong></p>
  
  <pre><code>function bdw_get_images() {
  
  $iPostID = $post-&gt;ID;
  $arrImages =&amp; get_children('post_type=attachment&amp;post_mime_type=image&amp;post_parent=' . $iPostID );
  
      if($arrImages) {
  
          $arrKeys = array_keys($arrImages);
  
          foreach($arrImages as $oImage) {
              $arrNewImages[] = $oImage;
          }
  
          for($i = 0; $i &lt; sizeof($arrNewImages) - 1; $i++) {
              for($j = 0; $j &lt; sizeof($arrNewImages) - 1; $j++) {
                  if((int)$arrNewImages[$j]-&gt;menu_order &gt; (int)$arrNewImages[$j + 1]-&gt;menu_order) {
                      $oTemp = $arrNewImages[$j];
                      $arrNewImages[$j] = $arrNewImages[$j + 1];
                      $arrNewImages[$j + 1] = $oTemp;
                  }
              }
          }
  
          $arrKeys = array();
  
          foreach($arrNewImages as $oNewImage) {
              $arrKeys[] = $oNewImage-&gt;ID;
          }
  
          $iNum = $arrKeys[0];
  
          $sImageUrl = wp_get_attachment_url($iNum);
  
          $sImgString = '&lt;img src="' . $sImageUrl . '" alt="Thumbnail Image" title="Thumbnail Image" /&gt;';
  
          echo $sImgString;
      }
  }
  </code></pre>
  
  <p>Then I can call the function with <code>bdw_get_images();</code>.</p>