I've got a code like:

<?php $loop = some array with posts;
      while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <div class="class">
                <p>(data)</p>
             </div>   
     <?php endwhile; ?>

Now I wish to alter the div class (from "class" to "class2") for that last loop. How to achieve that?

Example:

When "some array with posts" have finally 4 records then I am getting:

<div class="class"><p>data</p></div>
<div class="class"><p>data</p></div>
<div class="class"><p>data</p></div>
<div class="class"><p>data</p></div>

And I wish to get:

<div class="class"><p>data</p></div>
<div class="class"><p>data</p></div>
<div class="class"><p>data</p></div>
<div class="class2"><p>data</p></div> <!-- this one is different -->

I am searching for something which works always regardless of the number of array elements you will see.

Tanks!

<?php
$num_rows = mysql_num_rows($result1);
$i = 0;
while ($row = mysql_fetch_array($result1, MYSQL_ASSOC)) {
    $i++;
    /*
    your code
     */
    if ( $i == ( $num_rows - 1 ) )
        //you're on last line...    
}
?>

Obtained from http://www.phpbuilder.com/board/showthread.php?t=10359504 here, just adapt this to your requirements and it is simple to alter the last iteration informed

The very best you should do is most likely to place the ultimate call outdoors the while loop, and alter your while loop logic to ensure that it exits early. Rather than:

while ($post = getPost()) {
    printPost($post);
}

make a move like

$last = getPost();
$post = getPost();
while ($post != null) {
    printPost($last);
    $last = $post;
    $post = getPost();
}
printSpecial($post);

Edit: I have been presuming you don't know the amount of posts until you've iterated them over, which the interface that returns posts until they're exhausted is the only person available. For those who have a count and may access them by number, then your other suggestions works fine. Actually, if you're able to rely on the amount of posts being small, then you may be best simply to read them into an assortment and get it done having a for loop as opposed to a while loop.

<?php $loop = some array with posts;
      while ( $loop->have_posts() ) : $loop->the_post();
         $class = $loop->have_posts() ? 'class' : 'class2'; ?>
            <div class="<?php echo $class ?>">
                <p>(data)</p>
             </div>   
     <?php endwhile; ?>

For those who have treatments for the information structure of $loop you may convert int for an iterator and employ a CachingIterator on the top from it. If you do not control a little more difficult may be to construct an Iterator as wrapper:

<?php
class PostIterator implements Iterator {
    private $have_post;

    function __construct($loop) {
        $this->loop = $loop;
    }

    function rewind() {
        $this->have_post = $this->loop->have_posts(); // looks like we have to call "next" to get the first element, too
    }

    function next() {
        $this->have_post = $loop->have_posts();
    }

    function valid() {
        return $this->have_post;
    }

    function key() {
        return 0; // not used in this case, better implementation might have aocunter or such
    }

    function current() {
         return $this->loop->the_post();
    }
}

$ci = new CachingIterator(new PostIterator($loop)
foreach ($ci as $data) {
    if (!$ci->hasNext()) {
         // last element
    }
 }
 ?>

Make use of this code informed, immediately after while ( $loop->have_posts() ) : $loop->the_post();

<?php
$postCount = 0;

// Start of The Loop

if (++$postCount == 1) {
     // FIRST POST
} 

if ($postCount == sizeof($posts)) {
     // LAST POST IN LOOP
}

// End of The Loop
?>

Source:
http://wordpress.org/support/topic/last-post-in-the-loop?replies=4#post-954762