foreach output a set number of arrays only - php

How would I output only a set of numbers in an array, say if theres 10 arrays, I would only like to output 8 of them?
foreach($arrays as $array){
//do I use a for loop/
}
Thanks!

foreach is only the natural approach if you actually want to iterate over each item (as the name implies). However, you could do something like this:
$i = 0;
foreach($arrays as $array){
...
$i++;
if ($i == $limit) {
break;
}
}

$i = 0;
foreach($arrays as $array){
if($i < 8){
// do something
}
$i++;
}

foreach(array_slice($arrays, 0, 8) as $array){
//do I use a for loop/
}

You could use a foreach loop like this (already mentioned).
$i = 0;
foreach($arrays as $array){
//do I use a for loop/
if(++$i >= 8) break;
}
...or you can use a for loop, which is designed for doing actions a set number of times, e.g., 8 times.
for($i=0; $i < 8; $i++){
$array = $arrays[$i];
// body
}

Related

php foreach loop

I have two arrays in my code.
Need to perform a foreach loop on both of these arrays at one time. Is it possible to supply two arguments at the same time such as foreach($array1 as $data1 and $array2 as $data2)
or something else?
If they both the same keys, you can do this:
foreach($array1 as $key=>data1){
$data2 = $array2[$key];
}
Assuming both have the same number of elements
If they both are 0-indexed arrays:
foreach ($array_1 as $key => $data_1) {
$data_2 = $array_2[$key];
}
Otherwise:
$keys = array_combine(array_keys($array_1), array_keys($array_2));
foreach ($keys as $key_1 => $key_2) {
$data_1 = $array_1[$key_1];
$data_2 = $array_2[$key_2];
}
Dont use a foreach. use a For, and use the incremented index, eg. $i++ to access both arrays at once.
Are both arrays the same size always?
Then this will work:
$max =sizeof($array);
for($i = 0; $i < $max; $i++)
array[$i].attribute
array2[$i].attribute
If the arrays are different sizes, you may have to tweak your approach. Possibly use a while.
iterative methods:
http://php.net/manual/en/control-structures.while.php
http://php.net/manual/en/control-structures.for.php
http://php.net/manual/en/control-structures.do.while.php
use for or while loop e.g.
$i = 0;
while($i < count($ar1) && $i < count($ar2) ) // $i less than both length !
{
$ar1Item = $ar1[$i];
$ar2Item = $ar2[$i];
$i++;
}
No to my knowledge with foreach, but can be easily done with for:
<?php
$cond1 = 0<count($array1);
$cond2 = 0<count($array2);
for ($i=0;$cond1 || $cond2;$i++)
{
if ($cond1)
{
// work with $array1
}
if ($cond2)
{
// work with $array2
}
$cond1 = 0<count($array1);
$cond2 = 0<count($array2);
}
?>

PHP. How Can I check ALL array

I have this code:
$data[1] = "blablabla";
$data[2] = "blablablabla";
if (strlen($data) < 10)
{
// doing...
}
In this code I want to check all elements from array. How to do it?
foreach ($data as $element) {
if (strlen($element) < 10) {
// Do something
}
}
If you want to modify the data, use a reference (add a & before $element):
foreach ($data as &$element) {
if (strlen($element) < 10) {
// Do something to $element
$element = "something else";
}
}
If you don't want to use references directly, you can use a standard for loop with an indexer:
for ($i = 0; $i < count($data); $i++) {
if (strlen($data[$i]) < 10) {
// Do something with $data[$i]
$data[$i] = "something else";
}
}
Use array_walk function of PHP. There are many examples on the linked PHP manual page.
Also take a look at array_map and array_filter functions.
for ($i = 0;$i < count($data);$i++){
if (strlen($data[$i]) < 10){
// process
}
}

PHP explode - running loop through each array item

Here's the issue:
I retrieve the following data string from my database:
$row->exceptions = '1,2,3';
After explode I need the below code to check each one of the exploded pieces
$exceptions = explode(",", $row->exceptions);
//result is
//[0] => 1
//[1] => 2
//[2] => 3
for ($i = 0; $i <= $row->frequency; $i++) {
if ($exceptions[] == $i) {
continue;
} else {
//do something else
}
}
How can I make $exceptions[] loop through all keys from the exploded array so it evaluates if ==$i?
Thanks for helping.
It should suffice to substitute:
if($exceptions[] == $i)
with:
if(in_array($i,$exceptions))
By the way, it eliminates the need for a nested loop.
Ah, should be straightforward, no?
$exceptions = explode(",", $row->exceptions);
for ($i = 0; $i <= $row->frequency; $i++) {
foreach($exceptions as $j){
if($j == $i){
// do something
break;
}
}
}
I think I understand what you are asking. Here's how you would test within that loop whether the key equals $i.
for ($i = 0; $i <= $row->frequency; $i++)
{
foreach ($exceptions as $key => $value)
{
if ($key == $i)
{
continue;
}
}
}

php loop with countdown

Say i start my counter at 400. How would i execute a foreach loop that will run backwards until 0?
pseudocode
$i = 400;
foreach(**SOMETHING**)){
//do stuff
$i--;
}
for($i = 400; $i > 0; $i--)
{
// do stuff
}
other ways to do it:
$i = 400;
while($i > 0)
{
// do stuff
$i--;
}
or
$a = range(400, 1);
foreach($a as $i)
{
// do stuff
}
In case you really want to iterate backwards over an existing array you can use array_reverse():
foreach(array_reverse($myArray) as $myArrayElement){
// do stuff with $myArrayElement
}
how about a for loop
for($i = 400; $i > 0; $i--)
{
//stuff
}
foreach is used for iterating over sequences or iterators. If you need a conditional loop then use for or while.

selective iteration of array in php

Is there a way of iterating through an array but performing an operation on every other element?
ie If I have an array with 13 elements how do I do something to only elements 2,4,6,8,10 and 12?
foreach($array as $val) {
if(($i++ % 2) == 0) {
...do stuff here...
}
}
for ($i=1; $i<sizeof($array); $i+=2) {
// do stuff to $array[$i]
}
You can integrate it into a foreach loop too:
$i = 0;
foreach ($array as $v) {
if ($i++ & 1) continue;
// do stuff to $v
}
Note: $i & 1 is equivalent to ($i % 2) == 1 (or just $i % 2).
to fix cletuses answer for more speed and fix typos:
for ($i = 1, $j = count($array); $i < $j; $i += 2) {
// code
}
Another variation on the answers already posted... Similar to Phil Carter's answer. If the array has a numeric index you can use that in the foreach instead of managing a separate counter variable:
foreach ($array as $i => $v) {
if (! ($i % 2)) {
// do stuff to $v
}

Categories