循环时操纵数组

So $tr['tree'] is an array. $dic is an array stored as key values. I want to add the key source to that those arrays. It looks like the following code doesn't work as expected as I'm guessing $dic is a new instance of the array object inside $tr['tree'].

foreach($tr['tree'] as $dic){
    $dic['source'] = $tr['source']." > ".$dic['name'];
}

Note, I'm coming from python where this would work brilliantly. So how would I do this in PHP?

foreach() creates copies of the items you're looping on, so $dic in the loop is detached from the array. If you want to modify the parent array, the safe method is to use:

foreach($array as $key => $value) {
   $array[$key] = $new_value;
}

You could use a reference:

foreach($array as &$value) {
                  ^---
     $value = $new_value;
}

but that can lead to stupidly-hard-to-find bugs later. $value will REMAIN a reference after the foreach terminates. If you re-use that variable name later on for other stuff, you'll be modifying the array, because the var still points at it.