Using PHP foreach how would I get only the first 9 results in one foreach and only the second 9 in another.
Something like
foreach {$shops[1 - 9] as $shop) {
foreach {$shops[10 - 18] as
$shop) {
Any ideas?
Marvellous
Use array_slice()
:
foreach(array_slice($shops,0,9) as $shop){
// etc.
}
foreach(array_slice($shops,9,9) as $shop){
// etc.
}
How about
foreach (array_slice($shops, 0, 9) as $shop) {
...
}
and
foreach (array_slice($shops, 9, 9) as $shop) {
...
}
??
Use a for
loop instead?
for (int $i = 0; $i < 9; $i++)
{
$shop = $shops[$i];
}
Then you could do another with $i = 10..19
. If you must use foreach
then have a counter you increment and break;
or use array_slice
as others have suggested.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With