4

如何将新的键/值对添加到嵌套 foreach 循环内的现有数组中,并使该对在循环范围之外持续存在?

<?PHP
    include('magpierss/rss_fetch.inc');
    /*
        one, two, skip a few...
        $urls is an associative array with 
        database indices as keys and 
        URLs as values
    */

    foreach ($urls as $url_hsh)
    {
        $feed_id = $url_hsh[0];
        $url     = $url_hsh[1];

        echo $feed_id . "<br/>" . $url . "<br/>"; // 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'] . "<br/>"; // works as expected
        }

        foreach ($rss->items as $item)
        {
            echo $item['feed_id'] . "<br/>"; // nuthin..... 
        }
    }
?>

谢谢

4

1 回答 1

9

如果我理解正确,你想要的是这个(对于第一个循环):

foreach ($rss->items as &$item) {

&作为$item参考,您对其所做的任何更改都将反映在$rss->items

于 2009-02-14T02:45:14.213 回答