Modify array values in foreach loop Modify array values in foreach loop arrays arrays

Modify array values in foreach loop


There are 2 ways of doing this

foreach($questions as $key => $question){    $questions[$key]['answers'] = $answers_model->get_answers_by_question_id($question['question_id']);}

This way you save the key, so you can update it again in the main $questions variable

or

foreach($questions as &$question){

Adding the & will keep the $questions updated. But I would say the first one is recommended even though this is shorter (see comment by Paystey)

Per the PHP foreach documentation:

In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.


Surely using array_map and if using a container implementing ArrayAccess to derive objects is just a smarter, semantic way to go about this?

Array map semantics are similar across most languages and implementations that I've seen. It's designed to return a modified array based upon input array element (high level ignoring language compile/runtime type preference); a loop is meant to perform more logic.

For retrieving objects by ID / PK, depending upon if you are using SQL or not (it seems suggested), I'd use a filter to ensure I get an array of valid PK's, then implode with comma and place into an SQL IN() clause to return the result-set. It makes one call instead of several via SQL, optimising a bit of the call->wait cycle. Most importantly my code would read well to someone from any language with a degree of competence and we don't run into mutability problems.

<?php$arr = [0,1,2,3,4];$arr2 = array_map(function($value) { return is_int($value) ? $value*2 : $value; }, $arr);var_dump($arr);var_dump($arr2);

vs

<?php$arr = [0,1,2,3,4];foreach($arr as $i => $item) {    $arr[$i] = is_int($item) ? $item * 2 : $item;}var_dump($arr);

If you know what you are doing will never have mutability problems (bearing in mind if you intend upon overwriting $arr you could always $arr = array_map and be explicit.