Can i do this (Loop in Loop)? - php

How can i do something like this:
for ($i = 1; $i <= $_SESSION['variable'] * $_SESSION['variable2']; $i++) {
$listo[$i] = $preparado1[$i].
for ($i = 1; $i <= $_SESSION['variable2']; $i++) {
$preparadoex[$i];
};
}
But doesnt work, i think this:
for ($i = 1; $i <= $_SESSION['variable'] * $_SESSION['variable2']; $i++) {
for ($i = 1; $i <= $_SESSION['variable2']; $i++) {
$listo[$i] = $preparado1[$i].$preparadoex[$i];
}
}
The script is simply, i have two classes the first have 2 numbers and the two have 3 numbers i should "connect" all numbers (2) in the first class with all the numbers of the second class:
F-S
1-1,
1-2,
1-3,
2-1,
2-2,
2-3
Thanks

You're re-defining $i inside of the second loop, which conflicts with the first loop's counter. Use another variable, like $j:
for($i = 1;$i <=$_SESSION['variable'] * $_SESSION['variable2']; $i++ ){
for($j = 1; $j <=$_SESSION['variable2']; $j++){
$listo[] = $preparado1[$i] . $preparadoex[$j];
}
}

You just need to use a different variable name to increment the second 'for' loop.
for($i = 1;$i <=$_SESSION['variable'] * $_SESSION['variable2']; $i++ )
{
for($j = 1; $j <=$_SESSION['variable2']; $j++)
{
$listo[] = $preparado1[$i] . $preparadoex[$j];
}
}

You can have multiple nested loop, not just two. The only thing you have to make sure is that they each use a unique loop counter.
for($i = 1;$i <=$_SESSION['variable'] * $_SESSION['variable2']; $i++ ){
for($j = 1; $j <=$_SESSION['variable2']; $j++){
$listo[$i] = $preparado1[$i] . $preparadoex[$i];
}
}
In your test case your second for should use another variable like $j.

Related

How to loop a multiplication?

I want to find vector s. the formula is described in the picture below :
https://s.id/2UZKT (sorry i can't post the image)
the $bb_baru is an array which will be assigned the value in the table.
I've tried to loop, but the $multiplication_result variable is undefined.
for ($i=0; $i <= $data['jml_alternatif']; $i++) {
for ($i=0; $i <= ($this->input->post('iKriteria')-1); $i++) {
$multiplication_result *= number_format(pow($rangking[$i]['nilai_rangking'], $data['bb_baru'][$i]),4);
}
$data['vektor_s'][] = $multiplication_result;
}
the result of this case according by picture should be :
$vector_s[]=(4.1407,12.2393);
Declare the $multiplication_result variable before the second loop.
for ($i=0; $i <= $data['jml_alternatif']; $i++) {
$multiplication_result = 1;
for ($i=0; $i <= ($this->input->post('iKriteria')-1); $i++) {
$multiplication_result *= number_format(pow($rangking[$i]['nilai_rangking'], $data['bb_baru'][$i]), 4);
}
$data['vektor_s'][] = $multiplication_result;
}

For loop (php) that results in this: 12345678910987654321

My niece is trying to create one for-loop (php), that results in this:
* 12345678910987654321
example for loop she tried:
for ($i = 1; $i <= 10; $i++ , $i = 10; $i <= 1; $i--) {
echo $i . ' ';
}
She can only use if's and elseif's. I'm not a programmer and can't really help her. Any ideas how this could be achieved in php?
Any information would be greatly appreciated.
The key is to add a variable instead of a number, then reverse that number when $i hits 10.
for($i = 1, $j = 1; $i> 0; $i+=$j) // Start i at 1, and j at 1
{
echo $i;
if($i == 10)
$j = -1; // i has hit 10, so use -1 to start subtracting
}
Another possibility is to loop up to 20, printing $i for the ascending part and 20 - $i for the descending.
for ($i = 1; $i < 20; $i++) {
if ($i <= 10) {
echo $i;
} else {
echo 20 - $i;
}
}

add in loop in multi dimensional array

i am facing a problem
can some one suggest me
for ($i = 1; $i <= 2; $i++) {
$r2 = 0;
for ($t = 1; $t <= 2; $t++) {
echo $r2;
$r2++
}
}
output is 0101;
can i get output 0123 ??? please
if
for ($i = 1; $i <= 3; $i++) {
$r2 = 0;
for ($t = 1; $t <= 3; $t++) {
echo $r2;
$r2++
}
}
output is 010101;
can output 012345678 ??? please
and if
for ($i = 1; $i <= 4; $i++) {
$r2 = 0;
for ($t = 1; $t <= 4; $t++) {
echo $r2;
$r2++
}
}
output is 01010101;
can output 0123456789101112131415 ??? please
i think you understand
thanks
In all of these cases you are initializing $r2=0; in the inner loop. It should be outside the loop.
$r2=0;
for($i=1;$i<=2;$i++){
for($t=1;$t<=2;$t++){
echo $r2;
$r2++
}
}
This would produce "1234".
why are you using two nested for loops ?
why not just use one:
for ($i=0; $i<=15; $i++) echo $i . " ";
Try this:
$r2 = 10;
for($t = 0; $t <= $r2; $t++){
echo $r2;
}
Oh wait.. I get it now, why you have the two nested loops, you want to essentially raise a number to the power of 2 in order to control the number of values output. In that case, what you want is simply this:
// this is the variable you need to change to affect the number of values outputed
$n = 2;
// Square $n
$m = $n * $n;
// Loop $m times
for ($i = 0; $i < $m; $i++) {
echo $i;
}

decrese value of loop

I have this code, but i want in second loop a decrease of $p value. The first internal loop must be repeated three times, the second, two times and the last, one time. I am trying $p-- but without success.
Any idea ? thanks
$p = 3;
for ($i = 0; $i < 3; $i++) {
for ($o = 0; $o < $p; $o++) {
echo "something";
$p--;
}
}
Move your $p-- to outside the inner for loop:
$p = 3;
for ($i = 0; $i < 3; $i++) {
for ($o = 0; $o < $p; $o++) {
echo "something";
}
$p--;
}
Or better, just depend on the value of $i:
for ($i = 0; $i < 3; $i++) {
for ($o = 0; $o < 3 - $i; $o++) {
echo "something";
}
}
Or if you don't actually use $i:
for ($i = 2; $i >= 0; $i--) {
for ($o = 0; $o < $i; $o++) {
echo "something";
}
}
It's quite simple.
for ($i = 2; $i >= 0; $i--)
{
}
Set the starting number at the upper limit number, and then go down until equal to 0, $i minus 1;
You need to decrement $p outside the first loop

create var's with others

I want to create somthing like 100 var's which their names will be:
$numbr_1 = 1;
$numbe_2 = 2;
$number_3 = 3;
...
I won't write 100 vars of course, but there is a way to do it with foor loop or somthing? I thought about this:
for ($i = 1; $i <= 100; $i++)
$number_{$i} = $i;
You're talking about variable variables, and they are incredibly stupid to use. For one, they make debugging next to impossible.
What you want is an array:
for ($i = 1; $i <= 100; $i++) {
$numbers[$i] = $i;
}
Something like this should work:
for($i = 1 ; $i <= 100 ; $i++){
$var_name = "number_$i";
$$var_name = $i;
}
for($i=1;$i<=100;$i++) {
$j="number$i";
$$j = $i;
}
Why don't you use an array?
$number = array();
for ($i = 0; $i < 100; $i++)
{
$number[] = $i;
}
for($i = 1 ; $i <= 100 ; $i++){
${'number_'.$i} = $i;
}
Possible solution is usage of array.
$number = array();
for ($i = 1; $i <= 100; $i++) {
$number[$i] = $i;
}

Categories