3

I've been studying some test questions. One of the questions about array iteration. Here it is :

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!");
?>

A. Using a for loop

B. Using a foreach loop

C. Using a while loop

D. Using a do…while loop

E. There is no way to accomplish this goals

My answer is "of course foreach loop". But according to the answer sheet :

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.

I still think foreach is the best. And as long as I use it with key I can modify values.

<?php
foreach($myarray as $k=>$v){
    $myarray[$k] = "Modified ".$v;
}
?>

Do I miss something?

1
  • See this web Commented Nov 10, 2011 at 12:35

3 Answers 3

7

according to the answer sheet:

this option is not available, since foreach works on a copy of the array and would therefore result in added overhead

Nonsense.

If you need proof, you can take the values by reference:

<?php
foreach($myarray as &$v){
    $v = "Modified ".$v;
}
?>

I still think foreach is the best. And as long as i use it with key i can modify values.

I agree.

Do I miss something?

No.

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

3 Comments

"Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself" in the manual looks like a manual bug; someone's misinterpreted the way that the value is copied by default.
I have filed a documentation bug (#60255).
@Bahadır: The documentation bug has now been fixed, and this fix will take effect on the web documentation in due course. Please do show this thread to your teacher.
1

http://php.net/manual/en/function.array-walk.php

array_walk($myarray, "modify");

function modify($value)
{
  $value = "modified " . $value;
}

If you want to apply this to multiple arrays you can even use array_map()

2 Comments

"You have to use something else or something". Hmm.
Yes I guess it was confusing :) (removed it)
0

You could also do

<?php
foreach($myarray as &$v){
    $v = "Modified ".$v;
}
?>

Or in some cases if you have a function that is sufficient for the job:

$myarray = array_map( "trim", $myarray );

It will apply trim function to every element in the array and return the modified version.

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.