在foreach循环中将项添加到数组中失败


failed to add item into an array in the foreach loop

我是zend框架的新手,在尝试向数组中添加项时遇到了问题。

首先我创建了一个数据库表对象:

$questionTable = new xxx();
        $db = $questionTable->getAdapter();
        $sql = "select * from questions where value='v'";
        $res = $db->query($sql)->fetchAll();

$res是一个数组,在JSON[{‘a':1},{‘a':2},{‘a’:3}]中看起来像这样

然后我用了一个前臂:

foreach($res as $element)
        {
            $value = $element['a'];
            if($value == 2)
            {
                $element['extra'] = 10;

            }
        }

然后当我用打印出结果时

echo Zend_Json_Encoder::encode($res);

它仍然是[{’a':1},{’a':2},{’a’:3}]。

有人知道怎么修吗?

thx提前:)

只需在$element 中添加一个神奇的安培数

foreach($res as &$element)
----------------^