1

I am trying to change multidimensional array index in running foreach:

$array = array(
    array("apple", "orange"),
    array("carrot", "potato")
    );

$counter = 0;

foreach($array[$counter] as &$item) {
    echo $item . " - ";
    $counter = 1;
}

I supposed, that output would be apple - carrot - potato -, because in first run it takes value from zero array and then in next runs values from first array.

However, the output is apple - orange -

I tried add "&" before variable $item, but i guess it is not what I am looking for.

Is there any way to do it?
Thank you

// Well, i will try to make it cleaner:
This foreach takes values from $array[0], but in run i want to change index to 1, so in next repeat it will take values from $array[1]

Is that clear enough?

Note: I do not know how many dimensions my array has.

My purpose of this is not to solve this exact case, all I need to know is if is it possible to change source of foreach loop in run:

My foreach

$counter = 0;
foreach($array[$counter] as $item) {
 echo $item . " - ";
}

is getting values from $array[0] right now. But inside it, I want to change $counter to 1, so next time it repeats, it will get values from $array[1]

$counter = 0;
foreach($array[$counter] as $item) {
 echo $item . " - ";
 $counter = 1;
}

I see, it is kind of hard to explain. This is how foreach should work:
Index of $array is $counter

First run
$array[0] -> as $item = apple
echo apple
wait, now counter changes to 1

Second run
$array[1] -> as $item = carrot
echo carrot

Third run
$array[1] -> as $item = potato
echo potato

END

I am really trying to make it clear as much as possible :D

20
  • Pretty Unclear what you want to achieve over here. Commented May 5, 2016 at 9:37
  • foreach loop gets variable $array[0] and works on it Commented May 5, 2016 at 9:43
  • Do you want apple-carrot-potato or apple-orrange-carrot-potato ? Commented May 5, 2016 at 9:44
  • 1
    but for now you want first element from first array and all from secend? echo $array[0][0] . " - " . implode(" - ", $array[1]); example Commented May 5, 2016 at 10:57
  • 1
    foreach gets array as value. As Shady wrote, you cannot change the iteration index of foreach as the pointer to that is still pointing to the previous result. doing so won't change the pointer and continue as it is. you nee to use for or while loop, maybe recursion depending on your case. Commented May 6, 2016 at 10:20

2 Answers 2

1

When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. Now, by changing counter, you are changing the array (the inside one), but it is set to the first array again.

Changing the array in between may result in unexpected behaviour.

Source: Foreach: PHP.net

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

5 Comments

Well, that is good to know. Hmm. Could I do it by for loop then? Thanks
for($c=0; $c<2;$c++) { $item = $array[$c][0]; echo $item . " - "; } With the above code, source is changed using For loop, Just tweaking it a little with one extra counter will also solve the actual example you gave @Mamut
Of course, but can i change variable $c inside the loop? Like in my example above
$array = array( array("apple", "orange"), array("carrot", "potato"), array("mango", "doesn't matter"), array("grapes", "doesn't matter"), ); $counter = 0; for($c=0; $c<4;$c++) { $item = $array[$c][0]; $c++; echo $item . " - "; echo " "; } Sure you can
I did not even realize, there is for loop too :D this is not final version how it should work, but I will try something and i will come back then with solution or with another question :D
0

So finally, I found a time for this, and this is how you can change index of source array in running loop.

Thanks to you, I realized, that i can not do this by foreach, but there are many other loops.

If you were facing the same problem, here is my solution:

$array = array(
 array("apple", "orange"),
 array("carrot", "potato")
);

$counter = 0;
$x = 0;

while($x < count($array[$counter])) {
    echo $array[$counter][$x] . " - ";
    $x++;
    if($counter == 0) {
        $x = 0;
        $counter = 1;
    }
}

And the output is: apple - carrot - potato - as I wanted to achieve.

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.