I have a for loop that goes through a couple of arrays and assigns some values after doing some basic math. My question is: Is there a way to make sure that this for loop ONLY iterates while an additional condition is true?
for ($i = 0; $i < count($ci); $i++) {
$PIDvalue = $ci[$i]["PID"];
$datevalue = $pi[$i]["datetime"];
$pp_tcvalue = $ci[$i]["pp_tc"] - $pi[$i]["pp_tc"];
$pp_trvalue = $ci[$i]["pp_tr"] - $pi[$i]["pp_tr"];
$bp_tcvalue = $ci[$i]["bp_tc"] - $pi[$i]["bp_tc"];
$emailvalue = $ci[$i]["email"];
So I want something like this...
for ($i = 0; $i < count($ci); $i++) {
if($ci[$i]["email"] === $pi[$i]["email"]) {
$PIDvalue = $ci[$i]["PID"];
$datevalue = $pi[$i]["datetime"];
$pp_tcvalue = $ci[$i]["pp_tc"] - $pi[$i]["pp_tc"];
$pp_trvalue = $ci[$i]["pp_tr"] - $pi[$i]["pp_tr"];
$bp_tcvalue = $ci[$i]["bp_tc"] - $pi[$i]["bp_tc"];
$emailvalue = $ci[$i]["email"];
If they don't match, I would assign a value of "0" or something.
You could add an additional condition to the for-loop, making it pretty much unreadable:
for ($i = 0; $i < count($ci), $ci[$i]["email"] === $pi[$i]["email"]; $i++)
but I would rather break the loop:
for ($i = 0; $i < count($ci); $i++) {
if ($ci[$i]["email"] !== $pi[$i]["email"]) break;
$PIDvalue = ...
You can add that in your for condition, like that :
for ($i = 0; $i < count($ci) && $ci[$i]["email"] === $pi[$i]["email"]; $i++) {
When the second condition computes to false, the loop will stop running.
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.
I have the following code:
$extraPhoto_1 = get_field('extra_photo_1');
$extraPhoto_2 = get_field('extra_photo_2');
$extraPhoto_3 = get_field('extra_photo_3');
$extraPhoto_4 = get_field('extra_photo_4');
But I would like to rewrite it with a for loop, but I can't figure out how to put a variable within the value field. What I have so far is:
for($i = 1; $i < 5; $i++) {
${'extraPhoto_' . $i} = get_field('extra_photo_ . $i');
}
I've tried with an array like this:
$myfiles = array();
for ($i = 1; $i < 5; $i++) {
$myfiles["$extraPhoto_$i"] = get_field('extra_photo_ . $i');
}
Nothing seems to fix my problem. I'v searched on the PHP website (variable variable).
There is some bug in your code which not allowing. Use 'extra_photo_'. $i instead of 'extra_photo_. $i'
for($i = 1; $i < 5; $i++) {
$extraPhoto_.$i = get_field('extra_photo_'. $i);
}
You can build an array as defined below and than just call extract($myfiles) to access them as variables.
Again your syntax for the get field is incorrect you should append $i after the quotes.
$myfiles = array();
for ($i = 1; $i < 5; $i++) {
$myfiles["extraPhoto_".$i] = get_field('extra_photo_'.$i);
}
extract($myfiles);
If you want to create dynamic variable, use below code
for ($i = 1; $i < 5; $i++) {
${'extraPhoto_'.$i} = $_POST['extra_photo_'.$i];
}
if you want to assign variables to array use below code.
for ($i = 1; $i < 5; $i++) {
$myfiles["extraPhoto_$i"] = $_POST['extra_photo_'.$i];
/// $myfiles["extraPhoto_$i"] = get_field('extra_photo_'.$i);
}
and than to see if values are assign to new array or not, you can use below code.
echo "<pre>";print_r($myfiles);echo "</pre>";
I am new to PHP and for loops but I am having trouble racking my brain around the math for this. I am trying to write a loop that will create an array with 49 items. The items have two incrementing values within them. The 49 items are below:
M1s1t1url
M1s1t2url
M1s1t3url
M1s1t4url
M1s1t5url
M1s1t6url
M1s1t7url
M1s2t1url
M1s2t2url
M1s2t3url
M1s2t4url
M1s2t5url
M1s2t6url
M1s2t7url
M1s3t1url
M1s3t2url
M1s3t3url
M1s3t4url
M1s3t5url
M1s3t6url
M1s3t7url
M1s4t1url
M1s4t2url
M1s4t3url
M1s4t4url
M1s4t5url
M1s4t6url
M1s4t7url
M1s5t1url
M1s5t2url
M1s5t3url
M1s5t4url
M1s5t5url
M1s5t6url
M1s5t7url
M1s6t1url
M1s6t2url
M1s6t3url
M1s6t4url
M1s6t5url
M1s6t6url
M1s6t7url
M1s7t1url
M1s7t2url
M1s7t3url
M1s7t4url
M1s7t5url
M1s7t6url
M1s7t7url
As you can see there are three numbers in each item. The first number is a constant. The second number counts up to 7 then resets back to 1. The third number adds 1 every time the second number resets back to 1. Here is what I have below but I know I am off on the calculations.
for ($i = 1; $i < 8; $i = $i + 1) {
for ($u = 1; $u < 8; $u = $u + 1) {
$urln[] = 'M1s'.[$u].'t'.[$i].'url';
}
}
I am getting an array to string error.
<?php
for ($i = 1; $i < 8; $i++) {
for ($u = 1; $u < 8; $u++) {
$urln[] = 'M1s' . $u . 't' . $i . 'url';
}
}
<?php
$urln = array();
for ($i = 1; $i < 8; $i++) {
for ($u = 1; $u < 8; $u++) {
$urln[] = 'M1s' .$i. 't'. $u .'url';
}
}
foreach ($urln as $i) {
echo "$i\n";
}
?>
for ($i=40; $i>=30; $i--) //code will display data for top x row
for ($i=1; $i<=9; $i++) //code will display data for left y column
for ($i=29; $i>=21; $i--) //code will display data for bottom x row
for ($i=30; $i>=39; $i++) //code will display data for right y column
These 4 loops all do the same thing.
In my index.php im using "include" to get the 4 loops that are in 4 different files.
How can I make the for loop dynamic?
Algoritihm:
$i = (40,1,29,30) <--will be any of those 4
$maxlow = (30,9,21,39)
$check =(>,<) <--value depends on whether $i > or < $maxlow
$icrement = (--,++) <-- if $check is > then decrease, otherwise increment
for ($i; $i($check)=$maxlow; $i($increment) <---what i am trying to do
// $step is either 1 (incrementing) or -1 (decrementing)
foreach (range($begin, $maxlow, $step) as $i) {
}
Settings for given loop:
$diff = -1;
$start = 40;
$stop = 30 + $diff;
The loop itself, always like this:
for ($i = $start; $i != $stop; $i += $diff)
$low = [40,1,29,30](rand(0,3);
$high = [30,9,21,39](rand(0,3);
$modifier = ($low > $high) ? -1 : 1;
for($i = $low; ($i * $modifier) < ($high * $modifier); $i += $modifier)
{
doStuff();
}
why not just use for with switch inside, like this:
for ($i=1; $i >=39; $i++) {
switch($i) {
case ($i>=1 && $i<=9):
break;
...
...
case ($i>40):
//do something
break;
}
}
It will be much more readable, and easier to understand/edit in future.
In My last post I asked :
How to create dynamic incrementing variable using "for" loop in php? like wise: $track_1,$track_2,$track_3,$track_4..... so on....
whose answer I selected as
for($i = 0; $i < 10; $i++) {
$name = "track_$i";
$$name = 'hello';
}
and
for($i = 0; $i < 10; $i++) {
${'track_' . $i} = 'val'
}
Now, What If I need the Value of variable previous than the current variable?
for($i = 0; $i < 10; $i++) {
${'track_' . $i} = 'val'
if($i != 0){
$prev_val = ${'track_' . ($i - 1)}
}
}
But it's much better to use arrays for this, which are meant for this application.
$tracks = array();
for($i = 0; $i < 10; $i++){
$tracks[$i] = 'val';
if($i != 0){
$prev_val = $tracks[$i-1];
}
}
I guess the simples way would be to use two variables.
$name2 = "track_0";
for($i = 1; $i < 10; $i++) {
$name1 = $name2;
$name2 = "track_$i";
$$name1 = 'hello_previous';
$$name2 = 'hello_this';
}
Or if you explicitly use i = [0...10] to generate a variable name, you could simply write $name2 = "track_". $i; $name1 = "track_" . ($i - 1);
I know the others are saying just subtract by 1, but what if your list goes 1, 2, 4, 5, 8, 9? The previous of 8 is not 7, but 5, this following method (with a bit of modification to work as you require) will provide a way of getting the true previous value, and not a guessed one.
for($i = 0; $i < 10; $i++) {
${'track_' . $i} = 'val'
if(!empty($last_val))
// do what you want here
// set a var to store the last value
$last_val=$i;
}
if ($i != 0)
{
$prev = ${'track_' . ($i-1)} ;
}
?
${'track_' . ($i-1)}; won't suffice?