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

前端 未结 9 1942
轻奢々
轻奢々 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条回答
  •  情深已故
    2020-12-10 05:20

    Try

    $catalog = array();
    
    foreach( $store as $key => $value){
        $catalog[] = $value.".txt.gz";
    }
    
    
    print_r ($catalog);
    

    OR

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

    Depends on what you want to achieve

    Thanks :)

提交回复
热议问题