PHP:数组不保留值

The goal of this snip-it was to loop an array of of arrays describing resources. Then looping an array of caption information. If they match add that caption to this resource_node index.

The first echo print_r($resource); has the 'caption' index key.

The second echo print_r($this->resource_nodes); does not show the caption keys.

I have no idea what is going wrong in this snip-it.

foreach ($this->resource_nodes as $resource) {
    foreach ($this->captions as $caption) {
        if ($resource['attachment id'] === $caption['id']) {
            $resource['caption'] = $caption['content'];
            echo print_r($resource);
        }
    }
}
echo print_r($this->resource_nodes);

The comparison echo works. So I know that the captions are assigned to some array, and that the arrays are identical except for the missing caption after loop execution.

Any idea what is causing this?

Your $resource variable is being set by value, not by reference. Try:

foreach ($this->resource_nodes as &$resource) {

Note the '&'.