This is my code:
$arr = [
[step_1] => Array
(
[method] => getLastRealisesData
[description] => Get last realises data
[error] => null
[done] => 0
)
[step_2] => Array
(
[method] => downloadFile
[description] => Download file
[error] => null
[done] => 0
)
];
foreach($arr as $item){
$result = 0;
if($result == 0){
$item['done'] = 0;
$item['error'] = 'Error message';
break;
}
$result = 1;
}
My value did not update, why?
I need to update my value if I my result == 0
Mayby I need to use object or somethink else…
2
Answers
If you want to amend the array you are processing with the foreach loop, you have to use a reference, otherwise the foreach gets a copy of the array to process and any changes made within the loop to the array will dissapear at the end of the foreach as the copy is discarded.
From php documentation here:
Also you may be interested in what
by reference
means. Again, docs.Note also that reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().