PHP Foreach Change Original Array Values

PHP foreach change original array values

In PHP, passing by reference (&) is ... controversial. I recommend not using it unless you know why you need it and test the results.

I would recommend doing the following:

foreach ($fields as $key => $field) {
if ($field['required'] && strlen($_POST[$field['name']]) <= 0) {
$fields[$key]['value'] = "Some error";
}
}

So basically use $field when you need the values, and $fields[$key] when you need to change the data.

PHP foreach change array value

To be able to directly assign values to $value, you want to reference $value by preceding it with & like this:

foreach($array as $key => &$value){
$value = 12321; //the same as $array[$key] = 12321;
}

unset($value);

After the foreach loop, you should do unset($value) because you're still able to access it after the loop.

Note: You can only pass $value by reference when the array is a variable. The following example won't work:

foreach(array(1, 2, 3) as $key => &$value){
$value = 12321; //the same as $array[$key] = 12321
}

unset($value);



The php manual on foreach loops

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.

PHP foreach change two-dimensional original array values

You can use & which passes variable reference in the foreach loop.
You can try the below codes.

foreach ($myArray as $key => &$value) {
foreach ($value as $key2 => $value2) {
if($value2 == null) {
$value[$key2]="0";
}
}
}
print_r($myArray);

PHP changing array value in foreach

A foreach loop works by copying each value into a temporary variable.

If you want to edit the original array, you have two solutions :

Either pass the value with a reference, using & :

foreach ($items as &$item) {
/*...*/
$item['stock'] = $stock_quantity;
}

Or use the $key=>$value notation and edit the original array :

foreach ($items as $key => $item) {
/*...*/
$items[$key]['stock'] = $stock_quantity;
}

PHP - Nested Foreach Change Original Array Values

The first assignment works for me. But you can simplify it by using reference variables for the iteration variables.

foreach($array as &$value) {
foreach($value['customers'] as &$sValue) {
if($sValue['age'] < 35) {
$sValue['isYoung'] = true;
}
}
}

change initial array inside the foreach loop?

I don't think this is possible with a foreach loop, at least the way you wrote it : doesn't seem to just be the way foreach works ; quoting the manual page of foreach :

Note: Unless the array is referenced, foreach operates on a copy
of the specified array and not the
array itself.



Edit : after thinking a bit about that note, it is actually possible, and here's the solution :

The note says "Unless the array is referenced" ; which means this portion of code should work :

$i = 0;
$array = array('red', 'blue');
foreach($array as $key => & $value) {
$array[] = 'white';
echo $value . '<br />';
if ($i++ >= 5) {
break; // security measure to ensure non-endless loop
}
}

Note the & before $value.

And it actually displays :

red
blue
white
white
white
white

Which means adding that & is actually the solution you were looking for, to modify the array from inside the foreach loop ;-)



Edit : and here is the solution I proposed before thinking about that note :

You could do that using a while loop, doing a bit more work "by hand" ; for instance :

$i = 0;

$array = array('red', 'blue');

$value = reset($array);
while ($value) {
$array[] = 'white';
echo $value . '<br />';
if ($i++ >= 5) {
break; // security measure to ensure non-endless loop
}
$value = next($array);
}

Will get you this output :

red
blue
white
white
white
white

PHP foreach loop does not update value

The array is passed to the foreach as a copy - unless you pass it as a reference (indicate that by adding a & to the $value) it will not reflect the updates of the value you are doing in the first iteration.

Consider this:

foreach ($A as $j => &$line)
{
echo "j = $j line = $line\n";
echo "element $j is {$A[$j]}\n\n";

if ($j == 0)
$A[1] = "***" . $A[1];
}
/* Will output:

* j = 0 line = aa
* element 0 is aa
*
* j = 1 line = ***bb
* element 1 is ***bb
*/

You see the &line that mean we are passing into the block a reference and now any change is reflected to the array and not to a $line copy of the initial array you passed into the loop block.

Hope I was clear...

I notice the docs say that exactly:

It says that exactly in 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.



Related Topics



Leave a reply



Submit