"Another String",
"Hi, Mom!");
?> - Ответ: A) Using a for loop"> 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?<br><br><?php<br>$myarray = array ("My String",<br>"Another String",<br>"Hi, Mom!");<br>?> - Верный ответ: A) Using a for loop

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?

<?php
$myarray = array ("My String",
"Another String",
"Hi, Mom!");
?>


Вариант ответа 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


Комментарий: 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)";
}
?>

Комментарии

Имя пользователяТекст комментарияВведите код