sen5241b Posted November 20, 2022 Share Posted November 20, 2022 (edited) <!DOCTYPE html> <html> <body> <?php $colors = array("red", "green", "blue", "yellow"); foreach($colors as $x => $value): echo "$value <br>"; if ($x == 1) $colors[] = "pink"; endforeach; ?> </body> </html> Getting error PHP Parse error: syntax error, unexpected '$colors' (T_VARIABLE) in /home/KhtmoB/prog.php on line 10 Edited November 20, 2022 by sen5241b Quote Link to comment https://forums.phpfreaks.com/topic/315562-how-to-add-element-to-end-of-currently-iterating-array/ Share on other sites More sharing options...
Barand Posted November 20, 2022 Share Posted November 20, 2022 When you use foreach() you iterate through a copy of the array elements but you are adding 'pink' to the original array. To iterate through the original array you need to pass the values "by reference". For example $colors = array("red", "green", "blue", "yellow"); foreach($colors as $x => &$value): // note the "&" echo "$value <br>"; if ($x == 1) $colors[] = "pink"; endforeach; giving red green blue yellow pink Quote Link to comment https://forums.phpfreaks.com/topic/315562-how-to-add-element-to-end-of-currently-iterating-array/#findComment-1602787 Share on other sites More sharing options...
maxxd Posted November 20, 2022 Share Posted November 20, 2022 (edited) You can also use an ArrayIterator and the append method. From a random project I was working on, $u = new \ArrayIterator([1], \ArrayIterator::STD_PROP_LIST); while($u->valid()){ $x = 2 * $u->current() + 1; $y = 3 * $u->current() + 1; if($u->key() == $n){ // this is set elsewhere in the method; not really important for this example break; } $u->append($x); $u->append($y); $u->asort(); $u->next(); } Edited November 20, 2022 by maxxd Quote Link to comment https://forums.phpfreaks.com/topic/315562-how-to-add-element-to-end-of-currently-iterating-array/#findComment-1602788 Share on other sites More sharing options...
sen5241b Posted November 20, 2022 Author Share Posted November 20, 2022 (edited) For makes copy of array and works off of the copy. Appended elements are added to array but not to the time copy. You cannot process the appended elements in the initial for loop. <!DOCTYPE html> <html> <body> <?php $ptr = 0; $colors = array("red", "green", "blue", "yellow"); $colors[] = 'pink'; foreach ($colors as $key => $value) { echo "$key <br>"; echo "$value <br>"; if ($key == 1) { $colors[] = 'purple'; } } echo '<br>'; print_r($colors); ?> </body> </html> Edited November 20, 2022 by sen5241b Quote Link to comment https://forums.phpfreaks.com/topic/315562-how-to-add-element-to-end-of-currently-iterating-array/#findComment-1602791 Share on other sites More sharing options...
Barand Posted November 20, 2022 Share Posted November 20, 2022 24 minutes ago, sen5241b said: You cannot process the appended elements in the initial for loop. Yes you can - I just showed you how to do it. Quote Link to comment https://forums.phpfreaks.com/topic/315562-how-to-add-element-to-end-of-currently-iterating-array/#findComment-1602794 Share on other sites More sharing options...
sen5241b Posted November 22, 2022 Author Share Posted November 22, 2022 Yes, I meant without ArrayIterator it won't work. Also for loop works for ($x = 0; $x < 10; $x++) { if ($x == 5) array_push($arr, "fred"); } Quote Link to comment https://forums.phpfreaks.com/topic/315562-how-to-add-element-to-end-of-currently-iterating-array/#findComment-1602875 Share on other sites More sharing options...
Barand Posted November 22, 2022 Share Posted November 22, 2022 The first reply in this thread does it without array iterator - read it beyond the first sentence and look at the output. Quote Link to comment https://forums.phpfreaks.com/topic/315562-how-to-add-element-to-end-of-currently-iterating-array/#findComment-1602877 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.