0

I have this code block

<?php

$myArray = array('a', 'b', 'c');

foreach ($myArray as $k => $v) {
    echo $v;

    for ($i = 1; $i < 5; $i++) {
        if ($i == $k) {
            break;
        }

        echo $i; //a1234bc1 
    }
}


?>

and I have no problem with it until I get to the value after c. Shouldn't be 1234 instead of 1 ? am I missing something?

5
  • want this? a1234b1234c1234 Commented Mar 15, 2016 at 8:15
  • what is your expected output? Commented Mar 15, 2016 at 8:17
  • @FatalError because your $i and $k values are equal to 2(both are equal) at that stage. Commented Mar 15, 2016 at 8:20
  • 1
    yes,it is inside forloop at first it prints $i as 1 .then $i is incremented to 2. Commented Mar 15, 2016 at 8:25
  • Let us continue this discussion in chat. Commented Mar 15, 2016 at 8:27

4 Answers 4

2

Because,

  1. In the first iteration in foreach value of $k is 0 and there is no 0 in for loop, it covers all and print all 1 to 4, so output is a1234

  2. In the second iteration in foreach value of $k is 1 and for loop start from 1,so in if condition it is in first iteration, so loop stop in first iteration of for loop and print only b , so output is a1234b

  3. similarly in third iteration in foreach value of $k is 2 and for loop start from 1,so in if condition it is in second iteration, so loop stop in second iteration of for loop after print c1 , so output is a1234bc1

I think now its clear to you.

Sign up to request clarification or add additional context in comments.

Comments

0

You foreach ($myArray as $k => $v) loop is executed 3 times as $myArray has 3 Elements.

First run

$k = 0 which is the index of the first element, $v = "a"

echo $v; // Outputs a

Output of your loop

for ($i = 1; $i < 5; $i++) { ... }

Outputs all numbers from 1 to 5 and stops once the exact value of $k is met. $k is 0 so the condition (break) never gets triggered. Hence all numbers from 1 to 5 are echoed.

Output so far:

a1234

Second run

$k = 1 which is the index of the second element, $v = "b"

for ($i = 1; $i < 5; $i++) { ... }

Output of your loop

for ($i = 1; $i < 5; $i++)

Outputs all numbers from 1 to 5 and stops once the exact value of $k is met. As $k is 1 the break gets executed on first run of llop, hence no output.

Output so far:

a1234b

Third run

$k = 2 which is the index of the third element, $v = "c"

echo $v; // Outputs c

Output of your loop

for ($i = 1; $i < 5; $i++) { ... }

Outputs all numbers from 1 to 5 and stops once the exact value of $k is met. As $k euqals 2 this time the loop gets executed once outputting 1. On second run the break executes and terminates oputput

Final output:

a1234bc1

1 Comment

output for first iteration will be a1234 not a12345
0

1st Iteration:
1st loop
$k = 0; $v = a;
2nd loop
$i doesn't equal $k;
Output: a1234

2nd Iteration:
1st loop
$k = 1; $v = b;
2nd loop
$i equals $k(i.e 1);
Output: a1234b

3nd Iteration:
1st loop
$k = 2; $v = c;
2nd loop
$i equals $k(i.e 2);
Output: a1234bc1

Comments

0

Try this

$myArray = array('a', 'b', 'c');

foreach ($myArray as $k => $v) {
    echo $v;
    for ($i = 1; $i < 5; $i++) {
        echo $i; //a1234b1234c1234
    }
}

Remove if condition .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.