 <p>Given that <code>$index</code> is an integer you could just break out of the loop:</p> <pre><code>foreach ($images as $index =&gt; $image) { echo '&lt;li&gt;&lt;a href="'.$image['XLargeURL'].'"&gt;&lt;img src="'.$image['TinyURL'].'" alt="thumbnail"/&gt;&lt;/li&gt;'; if ($index &gt;= 5) { break; } } </code></pre>