如何将新的键/值对添加到嵌套的foreach循环内的现有数组中,并使该对保持在循环范围之外?
" . $url . "
"; // works as expected $rss = fetch_rss($url); // from 'magpierss/rss_fetch.inc' above foreach ($rss->items as $item) { $item['feed_id'] = $feed_id; echo $item['feed_id'] . "
"; // works as expected } foreach ($rss->items as $item) { echo $item['feed_id'] . "
"; // nuthin..... } } ?>
谢谢
如果我理解正确,你想要的是这个(对于第一个循环):
foreach ($rss->items as &$item) {
该&
将$item
是一个参考,你做任何更改将反映$rss->items