Clean way:
$arr50 = array_slice($obj, 0, 50);
foreach($arr50 as $element){
}
The usual way (this will only work for arrays with numerical indices and in ascending order):
for($i=0; $i<50 && $i<count($obj); $i++){
$element = $obj[$i];
}
Or, if you want to use foreach, you can have a use counter:
$counter = 0;
foreach($obj as $element){
if( $counter == 50) break;
$counter++;
}
source
share