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

share|improve this question
    
Pretty Unclear what you want to achieve over here. – Uchiha May 5 at 9:37
    
foreach loop gets variable $array[0] and works on it – ssnake May 5 at 9:43
    
Do you want apple-carrot-potato or apple-orrange-carrot-potato ? – Bsienn May 5 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 – ssnake May 5 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. – Bsienn May 6 at 10:20
up vote 1 down vote accepted

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

share|improve this answer
    
Well, that is good to know. Hmm. Could I do it by for loop then? Thanks – Mamut May 5 at 12:39
    
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 – Shady23 May 5 at 12:44
    
Of course, but can i change variable $c inside the loop? Like in my example above – Mamut May 5 at 12:47
1  
$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 – Shady23 May 5 at 12:55
    
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 – Mamut May 5 at 13:06

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.