-1

I have this code but does not work.

$arr1 = array('test1', 'test2', 'test3');
$arr2 = array(1, 2, 3);

foreach ($arr1 as $x) {
    $m = array_shift($arr2);

    //at this point, $arr2 remains unchanged, why?

    continue;
}

$arr2 does not seem to change within and in between iterations. Any insights will be highly appreciated. Thanks!

3
  • possible duplicate of change initial array inside the foreach loop? Commented Feb 8, 2015 at 12:12
  • I Cannot Reproduce your issue; this question is Off-topic. Commented Oct 16, 2022 at 3:00
  • "at this point, $arr2 remains unchanged, why?" What evidence do you have that $arr2 is unchanged? Please post code that indicates this definitively. Commented Oct 16, 2022 at 3:26

1 Answer 1

0

This does work. This code here:

$arr1 = array('test1', 'test2', 'test3');
$arr2 = array(1, 2, 3);

foreach ($arr1 as $x) {
    $m = array_shift($arr2);

    print_r($arr2);

    continue;
}

Will print this:

Array ( [0] => 2 [1] => 3 ) Array ( [0] => 3 ) Array ( ) 
Sign up to request clarification or add additional context in comments.

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.