Using PHP foreach, how would I get only the first 9 results in one foreach and only the second 9 in another.
Sort of
foreach {$ stores [1 - 9] as $ shop) {
foreach {$shops[10 - 18] as $shop) {
Any ideas?
Wonderful
Use array_slice() :
array_slice()
foreach(array_slice($shops,0,9) as $shop){ // etc. } foreach(array_slice($shops,9,9) as $shop){ // etc. }
Use a for loop instead?
for (int $i = 0; $i < 9; $i++) { $shop = $shops[$i]; }
Then you can do something else with $i = 10..19 . If you must use foreach , use the counter you increment and break; or use array_slice as others suggested.
$i = 10..19
foreach
break;
array_slice
What about
foreach (array_slice($shops, 0, 9) as $shop) { ... }
and
foreach (array_slice($shops, 9, 9) as $shop) { ... }
??
foreach (array_chunk($shops, 9) as $section) { // Do some logic on each section foreach ($section as $shop) { // Do some logic on each shop } }
using array_slice you can split your array into two, then you can do it
$my_array = array('1','2',...,'18'); $first_array = array_slice($my_array,0,9); $second_array = array_slice($my_array,9,18);
Use the for loop instead.
for ($i = 0; $i < 9; $i++) { $shop = $shops[$i]; } for ($i = 9; $i < 18; $i++) { $shop = $shops[$i]; }
Source: https://habr.com/ru/post/892754/More articles:Why sscanf does not work properly with the bool type - c ++Using your Google account as a login ... what's next? - androidPossible location-based installation in WPF? - .nethttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/892752/java-solutions-to-record-replay-executed-code-in-a-jvm&usg=ALkJrhi4hZfy0LxSuJrKQx2K5tvOpeEXkAAndroid development: find out which application is currently playing - androidCan a C implementation implicitly include standard headers when another header is included? - cApache Commons Email and SMTP Connection Reuse - javaIs this a proper C declaration? If so, why is it not working? - cCreate Addon to modify JavaScript data before executing in Firefox. - javascriptHow to change C ++ acceleration posix_time :: ptime to milliseconds? - c ++All Articles