0

I am new to zend framework and i am having a problem while i was trying to add items into an array.

first i created a db table object:

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

the $res is an array which looks like this in JSON [{'a':1},{'a':2},{'a':3}]

then i used a foreach :

foreach($res as $element)
        {
            $value = $element['a'];

            if($value == 2)
            {

                $element['extra'] = 10;



            }
        }

then when i print out the result using:

echo Zend_Json_Encoder::encode($res);

it remains [{'a':1},{'a':2},{'a':3}].

does anyone know how to fix it?

thx in advance:)

1 Answer 1

2

Just add a magical ampersand to your $element

foreach($res as &$element)
----------------^
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.