<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: PHP ForEach Last Item, Last Loop, Last Iteration</title>
	<atom:link href="http://blog.actsmedia.com/2009/09/php-foreach-last-item-last-loop/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.actsmedia.com/2009/09/php-foreach-last-item-last-loop/</link>
	<description>Acts Media specializes in Wed Design, Web Development, and Flash/Flex Authoring</description>
	<lastBuildDate>Tue, 17 Aug 2010 21:49:11 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
	<item>
		<title>By: Ahmad Shukr</title>
		<link>http://blog.actsmedia.com/2009/09/php-foreach-last-item-last-loop/comment-page-1/#comment-1155</link>
		<dc:creator>Ahmad Shukr</dc:creator>
		<pubDate>Tue, 17 Aug 2010 21:49:11 +0000</pubDate>
		<guid isPermaLink="false">http://blog.actsmedia.com/?p=140#comment-1155</guid>
		<description>//much simpler , time less algorithm 
$i = 0;
$length = count($arrray)  - 1;
 
foreach($array as $key =&gt; $value){
    if( $i != $length )
    {
      echo &#039;Woooooooohooo iam not the last&#039;;
   }
  else
  {
    echo &#039;sadly iam the last one :(&#039;;
  }
  $i++;
}</description>
		<content:encoded><![CDATA[<p>//much simpler , time less algorithm<br />
$i = 0;<br />
$length = count($arrray)  &#8211; 1;</p>
<p>foreach($array as $key =&gt; $value){<br />
    if( $i != $length )<br />
    {<br />
      echo &#8216;Woooooooohooo iam not the last&#8217;;<br />
   }<br />
  else<br />
  {<br />
    echo &#8216;sadly iam the last one <img src='http://blog.actsmedia.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> &#8216;;<br />
  }<br />
  $i++;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://blog.actsmedia.com/2009/09/php-foreach-last-item-last-loop/comment-page-1/#comment-392</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Thu, 21 Jan 2010 04:48:14 +0000</pubDate>
		<guid isPermaLink="false">http://blog.actsmedia.com/?p=140#comment-392</guid>
		<description>Thanks very much for this code. It didn&#039;t work for me.
I have the following array:
&lt;code&gt;
[field_author] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [value] =&gt; 529
                    [view] =&gt; Barak Obama
                )

            [1] =&gt; Array
                (
                    [value] =&gt; 530
                    [view] =&gt; Dmitry Medvedev
                )

            [2] =&gt; Array
                (
                    [value] =&gt; 531
                    [view] =&gt; Gordon Brown
                )

        )
&lt;/code&gt;

...and modified your code as follows:
&lt;code&gt;
$last_item = end($node-&gt;field_author);
$last_item = each($node-&gt;field_author);
reset($node-&gt;field_author);
foreach($node-&gt;field_author as $key =&gt; $value){
	// code executed during standard iteration
	print ($value[&#039;view&#039;].&#039;, &#039;);
	if($node-&gt;field_author == $last_item[&#039;value&#039;] &amp;&amp; $key == $last_item[&#039;key&#039;]){
		// code executed on the 
                // last iteration of the foreach loop
     	print $value[&#039;view&#039;];				
	}
}
&lt;/code&gt;

I get the following result:
Barak Obama, Dmitry Medvedev, Gordon Brown, 

I don&#039;t want the last comma 
Any suggestions?
Thanks again,
Scott

PS. What I&#039;d really like to be able to do is account for 1, 2, 3, 4 or more author cases, each having their own link. For example:
Barak Obama
Barak Obama and Dmitry Medvedev
Barak Obama, Dmitry Medvedev and Gordon Brown
Barak Obama, Dmitry Medvedev, Nicolas Sarkozy and Gordon Brown</description>
		<content:encoded><![CDATA[<p>Thanks very much for this code. It didn&#8217;t work for me.<br />
I have the following array:<br />
<code><br />
[field_author] =&gt; Array<br />
        (<br />
            [0] =&gt; Array<br />
                (<br />
                    [value] =&gt; 529<br />
                    [view] =&gt; Barak Obama<br />
                )</p>
<p>            [1] =&gt; Array<br />
                (<br />
                    [value] =&gt; 530<br />
                    [view] =&gt; Dmitry Medvedev<br />
                )</p>
<p>            [2] =&gt; Array<br />
                (<br />
                    [value] =&gt; 531<br />
                    [view] =&gt; Gordon Brown<br />
                )</p>
<p>        )<br />
</code></p>
<p>&#8230;and modified your code as follows:<br />
<code><br />
$last_item = end($node-&gt;field_author);<br />
$last_item = each($node-&gt;field_author);<br />
reset($node-&gt;field_author);<br />
foreach($node-&gt;field_author as $key =&gt; $value){<br />
	// code executed during standard iteration<br />
	print ($value['view'].', ');<br />
	if($node-&gt;field_author == $last_item['value'] &amp;&amp; $key == $last_item['key']){<br />
		// code executed on the<br />
                // last iteration of the foreach loop<br />
     	print $value['view'];<br />
	}<br />
}<br />
</code></p>
<p>I get the following result:<br />
Barak Obama, Dmitry Medvedev, Gordon Brown, </p>
<p>I don&#8217;t want the last comma<br />
Any suggestions?<br />
Thanks again,<br />
Scott</p>
<p>PS. What I&#8217;d really like to be able to do is account for 1, 2, 3, 4 or more author cases, each having their own link. For example:<br />
Barak Obama<br />
Barak Obama and Dmitry Medvedev<br />
Barak Obama, Dmitry Medvedev and Gordon Brown<br />
Barak Obama, Dmitry Medvedev, Nicolas Sarkozy and Gordon Brown</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Corey</title>
		<link>http://blog.actsmedia.com/2009/09/php-foreach-last-item-last-loop/comment-page-1/#comment-157</link>
		<dc:creator>Corey</dc:creator>
		<pubDate>Tue, 03 Nov 2009 23:21:14 +0000</pubDate>
		<guid isPermaLink="false">http://blog.actsmedia.com/?p=140#comment-157</guid>
		<description>If you&#039;re worried about runtime, the worst case scenario for this is O(n). A hash table could be useful if you&#039;re expecting lots of values.</description>
		<content:encoded><![CDATA[<p>If you&#8217;re worried about runtime, the worst case scenario for this is O(n). A hash table could be useful if you&#8217;re expecting lots of values.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ahmed Gaber</title>
		<link>http://blog.actsmedia.com/2009/09/php-foreach-last-item-last-loop/comment-page-1/#comment-144</link>
		<dc:creator>Ahmed Gaber</dc:creator>
		<pubDate>Sun, 25 Oct 2009 18:32:40 +0000</pubDate>
		<guid isPermaLink="false">http://blog.actsmedia.com/?p=140#comment-144</guid>
		<description>I&#039;ve spent hours looking for this solution,
nice working solution, but I wonder about the running time with large algorithms

any way, 
Nice work</description>
		<content:encoded><![CDATA[<p>I&#8217;ve spent hours looking for this solution,<br />
nice working solution, but I wonder about the running time with large algorithms</p>
<p>any way,<br />
Nice work</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anler</title>
		<link>http://blog.actsmedia.com/2009/09/php-foreach-last-item-last-loop/comment-page-1/#comment-119</link>
		<dc:creator>anler</dc:creator>
		<pubDate>Sun, 11 Oct 2009 09:27:57 +0000</pubDate>
		<guid isPermaLink="false">http://blog.actsmedia.com/?p=140#comment-119</guid>
		<description>great!</description>
		<content:encoded><![CDATA[<p>great!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

