Jump to content

[SOLVED] Like foreach, but I only want it happening a few times.


jlange

Recommended Posts

Maybe its something really simple, but I'd like to get a foreach going for items in an array, but I only want it happening a set number of times. I'm not really that much of an expert on loops (obviously), but I understand PHP, so go easy on me but not too easy!

You could do something like:

 

<?php

$count = 0;
foreach ($value as &$val) {
    //code
    if ($count >= 5) {
        break;
    } else {
        $count++;
    }
}

?>

 

But if you don't need to use foreach:

 

<?php
for($i=0;$i<5;$i++){
    print $array[$i]."<br>";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.