PHP Test online
10. What is the best way to iterate through the $myarray array, assuming you want to modify the value of each element as you do?Вариант ответа 1 - A) Using a for loop Вариант ответа 2 - B) Using a foreach loop Вариант ответа 3 - C) Using a while loop Вариант ответа 4 - D) Using a do…while loop Вариант ответа 5 - E) There is no way to accomplish this goal Comments: Normally, the foreach statement is the most appropriate construct for iterating through an array. However, because we are being asked to modify each element in the array, this option is not available, since foreach works on a copy of the array and would therefore result in added overhead. Although a while loop or a do…while loop might work, because the array is sequentially indexed a for statement is best suited for the task, making Answer A correct: <?php $myarray = array ("My String", "Another String", "Hi, Mom!"); for($i = 0; $i < count($myarray); $i++) { $myarray[$i] .= " ($i)"; } ?> |
Comments
| User name | Comment |
|---|
Русский