I have an array being returned from the database that looks like so:

$data = array(201 => array('description' => blah, 'hours' => 0),
              222 => array('description' => feh, 'hours' => 0);

In the next bit of code, I'm using a foreach and checking the for the key in another table. If the next query returns data, I want to update the 'hours' value in that key's array with a new hours value:

foreach ($data as $row => $value){
   $query = $db->query('SELECT * FROM t WHERE id=$row');
   if ($result){
      $value['hours'] = $result['hours'];
   }

It's all fine except that I've tried just about every combination of declarations for the foreach loop, but I keep getting the error that the $value['hours'] is an invalid reference. I've tried declaring $value[] ... but that doesn't work either. I don't need to iterate through $value so another foreach loop isn't necessary.

Surely this is easier than my brain is perceiving it.

Here's the whole snippet:

foreach($_gspec as $key => $value){

            $sql = sprintf('SELECT * FROM List WHERE specialtyID=%s', $key);
            $query = $db->query($sql);

            if ($query->num_rows() !== 0){

                $result = $query->row_array();
                $value['hours'] = $result['hours'];

            }
        }

Comments

What are $result and $query variables?

Written by vooD

Accepted Answer

You want

$data[$row]['hours'] = $result['hours']

$row would be better named as $key (that is what it is!)

Some people would suggest using pointers, but I find this way makes more sense to me.

Written by adam
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki