-1

I have the following problem:

I have some variables in my script. Now I want to process them in a for loop, but don't know how I have to solve my little problem.

My idea was to change the number (in the name) of my variable each time.

This is my script

<?php

$tropfenzahl = 2;

$v1_d1 = 10;
$v1_w2 = 20;
$v1_d2 = 30;
$v1_w3 = 40;

   for($i = 1; $i <= $tropfenzahl; $i++) {
      echo $v1_d1;
      echo $v1_w2;
   }
?>

The next time I want to echo $v1_d2 and $v1_w3 (up one number).

I think that the solution is very easy, but I don't get it right now =/

Have a nice one!

Fachinformatiker
  • 160
  • 1
  • 17

4 Answers4

3

If you don't want to loop through an array but actually do need to loop through separate variables for whatever reason, you can keep the variables you want to use as strings and then use a double $$ to access them:

$tropfenzahl = 4;

$v1_d1 = 10;
$v1_w2 = 20;
$v1_d2 = 30;
$v1_w3 = 40;

$d_variable = "v1_d1";
$w_variable = "v1_w2";

for($i = 1; $i <= $tropfenzahl; $i++) {
    echo $$d_variable;
    echo $$w_variable;

    $d_variable++;
    $w_variable++;
}
rickdenhaan
  • 10,857
  • 28
  • 37
  • If this is the actual code, then yes, of course OP should just use an array. But who knows, this might actually be a proper MCVE for a change. The real application might be very large and take weeks to refactor to use an array instead of separate variables. – rickdenhaan Jan 05 '18 at 22:49
2

Use arrays. http://php.net/manual/en/language.types.array.php

$tropfenzahl = 4;
$v1_d = [10, 30, 50, 70];
$v1_w = [20, 40, 60, 80];

for($i = 0; $i < $tropfenzahl; $i++) {
  echo $v1_d[$i];
  echo $v1_w[$i];
}
mozkomor05
  • 1,367
  • 11
  • 21
  • Thank you for your effort, but @rickdenhaan 's solution is the best for me, because I have everything in seperate variables (I have a bunch of them) Or is using an array better (faster for example)? – Fachinformatiker Jan 05 '18 at 22:45
  • If you have two rows of numbers, it is much better to use a arrays than to have several variables with a different number at the end. The array is much clearer and it is also important to remember that each variable occupies some space. – mozkomor05 Jan 05 '18 at 22:51
  • I see what you mean, but the problem is, that I get the values like so `$v1_x_d1 = $_GET['v1_d1']; $v1_d1 = $v1_x_d1 * 1000;` so I'm storing them in variables. I have to see how I can get them in an array and multiply them by 1000 like in the example. – Fachinformatiker Jan 05 '18 at 22:55
1

If your just echo'ing out a range of numbers im baffled as to why your not simply doing:

echo implode(range(10, 40, 10));

https://3v4l.org/lquQQ

If you have an arbitrary set of variables which you want to loop over then use the compact() function to put them into an array.

<?php
$v1_d1 = 10;
$v1_w2 = 20;
$v1_d2 = 30;
$v1_w3 = 40;

foreach (compact('v1_d1', 'v1_w2', 'v1_d2', 'v1_w3') as $var) {
    echo $var;
}

https://3v4l.org/1dYIY

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • Sorry for confusing you! my intention is not to echo the numbers, this was only for the example. My code is processing the numbers and controlling an electronic water valve (it's opening and closing it) the numbers are the opening time and the delay (and there are some more numbers, I just showed 4 of them =) Thanks for your time and I wish you a very nice weekend =) – Fachinformatiker Jan 05 '18 at 22:52
0

You are looking at this the wrong way . Maybe try this out

<?php

$tropfenzahl = 4;

$myArray = array(10,20,30,40);

foreach ($myArray  as $numbers) {
    if ($tropfenzahl == 4) {
        # do something
    }else{
        # Do something else
    }
}

?>

if you want to loop trough numbers they need to be in an array

Frosty
  • 299
  • 5
  • 31
  • Thank you for your effort, but @rickdenhaan 's solution is the best for me, because I have everything in seperate variables (I have a bunch of them) Or is using an array better (faster for example)? – Fachinformatiker Jan 05 '18 at 22:45