Search code examples
phpforeachlimit

PHP: Limit foreach() statement?


How can i limit a foreach() statement? Say i only want it to run the first 2 'eaches' or something?


Solution

  • You can either use

    break;
    

    or

    foreach() if ($tmp++ < 2) {
    }
    

    (the second solution is even worse)