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.
Related
Why this piece of code works when it is clearly wrong in the second for loop (for ($i==0; $i<$parts; $i++) {)?
Does php allows for multiple comparisons inside for loops?
function split_integer ($num,$parts) {
$value = 0;
$i = 0;
$result = [];
$modulus = $num%$parts;
if ($modulus == 0) {
for($i = 0; $i < $parts; $i++)
{
$value = $num/$parts;
$result[] = $value;
}
} else {
$valueMod = $parts - ($num % $parts);
$value = $num/$parts;
for ($i==0; $i<$parts; $i++) {
if ($i >= $valueMod) {
$result[] = floor($value+1);
} else {
$result[] = floor($value);
}
}
}
return $result;
}
Code for ($i==0; $i < $parts; $i++) runs because $i==0 has no impact on loop.
In normal for loop first statement just sets $i or any other counter's initial value. As you already set $i to 0 earlier, your loop runs from $i = 0 until second statement $i < $parts is not true.
Going further, you can even omit first statement:
$i = 0;
for (; $i < 3; $i++) {
echo $i;
}
And loop will still run 3 times from 0 to 2.
So I have a for loop that is decreasing...
for ($i=count($array); i>0; $i--;)
{
if(condition)
{DO SOMETHING like print the element in a decreasing manner}
if(enter ending iteration condition here after xth element) break;
}
that pretty much sums up my question. How do I formulate the ending iteration - let's say after 5 elements printed I want to stop the iteration.
$j = 0;
for ($i=count($array); $i>0; $i--)
{
if(condition)
{
DO SOMETHING like print the element in a decreasing manner;
$j++;
}
if($j > 4){
break;
}
}
Try to reverse the count of the loop. Instead of decreasing, try to increase so you will have a count of how many items are being printed.
<?php
for ($i = 0; $i < count($array); $i++)
{
if(condition)
{
/* DO SOMETHING like print the element in a decreasing manner */
}
/* replace (nth) with the needed number */
if($i === (nth)) break;
}
You could set the limit based on the count, like:
$loop_limit = 5;
$array_count = count($array);
$last = $array_count - $loop_limit;
for ($i = $array_count; $i >= $last ; --$i) {
if ( $i == $last ) {
//Do whatever you need at this point
}
//do the normal loop action
}
what I want to do is run this for loop and within there is a foreach searching the positions. what I want to do is once there it returns false I want it to break and save the position of $i in a variable. I'm using simple_html_dom.php but I don't think that matters since this is more of a basic programming problem.
for($i = $0; $i < $20; $i++){
foreach($html->find('div[class=cate_link]',$i) as $a){
if (strpos($a->plaintext,'+info') == false){
break;
}
}
//this is not valid, but essentialy this is what I want to do.
$stop = $i;
}
To break multiple levels in a loop you simply specify the levels, eg, break 2 - see the manual on break - http://www.php.net/manual/en/control-structures.break.php.
As such your code might work as
for($i = $0; $i < $20; $i++){
foreach($html->find('div[class=cate_link]',$i) as $a){
if (strpos($a->plaintext,'+info') == false){
$stop = $i; // Set variable
break 2; // break both loops
// or alternatively force the outer loop condition to expire
//$i = 21; // Force the outer loop to exit
//break;
}
}
}
I have expanded to question to set $i = 21 to break the outer loop with a single break.
Untested code but syntax checked...
<?php
// Untested code...
// Assume that you WILL break out of the loops...
$currentForIdx = -1; // so we can test that 'for' loop actually ran
$quitLoops = false;
for($i = 0; $i < $20 && !quitLoops; $i++) {
$currentForIdx = $i; // in case we break out of the loops
foreach($html->find('div[class=cate_link]',$i) as $a){
if (strpos($a->plaintext,'+info') == false) {
$quitLoops = true;
break;
}
}
}
// test $quitLoops and $currentForIdx to work out what happened...
?>
I havent tested this, but I would try something like this:
for($i = $0; $i < $20; $i++){
$stop = false;
foreach($html->find('div[class=cate_link]',$i) as $a){
if (strpos($a->plaintext,'+info') == false){
$stop = $i;
}
}
if ($stop !== false) {break;}
}
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
}
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
}
}