title: PHP function get the last inserted data in the array
body: <p>I know you guys going to say this might be possible duplicate to <a href="https://stackoverflow.com/questions/3275082/php-get-index-of-last-inserted-item-in-array">PHP get index of last inserted item in array</a></p>
  
  <p>But I've tried and not succeed to get my function work and I'm trying to figure it out why.</p>
  
  <p>I've a function which will grab the thumbnail attachment from the content of Wordpress post.</p>
  
  <p>The code working fine with a post that have only one image, but if there are more than one image in the post it will display the first image correctly from the <code>array</code>, but I don't know why it stores the image in reverse. Meaning the first image of the post stored last and the last store first. I'm geussing this is maybe how the <code>media</code> function of Wordpress work.</p>
  
  <p>Anyway I just need to get the last inserted data from function even if it has one or more than one image stored in the <code>array</code></p>
  
  <pre><code>// Get Image Attachments
  function sa_get_image($postid=0, $size='thumbnail') { 
      if ($postid&lt;1)
      $postid = get_the_ID();
      $thumb = get_post_meta($postid, "thumb", TRUE); 
      if ($thumb != null or $thumb != '') {
          echo $thumb;
      }
      elseif ($images = get_children(array( 
          'post_parent' =&gt; $postid,
          'post_type' =&gt; 'attachment',
          'numberposts' =&gt; '1',
          'post_mime_type' =&gt; 'image', )))
          foreach($images as $image) {
              $thumbnail=wp_get_attachment_image_src($image-&gt;ID, $size);
              ?&gt;
      &lt;?php echo $thumbnail[0]; ?&gt; // here is the problem where the image display.
      &lt;?php }
  
  }
  </code></pre>