Changing value inside foreach loop doesn't change value in the array being iterated over

前端 未结 9 1918
轻奢々
轻奢々 2020-12-10 04:31

Why does this yield this:

foreach( $store as $key => $value){
$value = $value.\".txt.gz\";
}

unset($value);

print_r ($store);

Array
(
[1] => 101Phon         


        
9条回答
  •  Happy的楠姐
    2020-12-10 05:12

    The $value variable in the array is temporary, it does not refer to the entry in the array.
    If you want to change the original array entry, use a reference:

    foreach ($store as $key => &$value) {
                           //  ^ reference
        $value .= '.txt.gz';
    }
    

提交回复
热议问题