Forum Moderators: coopster

Message Too Old, No Replies

How to limit array records?

         

toplisek

7:36 pm on May 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have code to show ALL array values like:
[PHP]

if(isset($var) && is_array($var) && count($var) > 0) {
foreach($var as $i=>$element) {


}


}

[/PHP]

How to set foreach function to show only first 3 elements (limited records...)?

Matthew1980

8:02 am on May 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there toplisek,

Forgive me for suggesting this, I am tired, but wouldn't you just set a counter up so that on the third iteration of the loop you would use a break; to exit the loop, or even get a goto going so that when condition is true your forwarded to another part of your script?

Cheers,
MRb

toplisek

11:24 am on May 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thank you. I have used array_slice function.