I have an array returned from a database that looks like this:
$data = array(201 => array('description' => blah, 'hours' => 0), 222 => array('description' => feh, 'hours' => 0);
In the next bit of code, I use foreach and check the key for another table. If the following query returns data, I want to update the "hours" value in this key array with the new hours value:
foreach ($data as $row => $value){ $query = $db->query('SELECT * FROM t WHERE id=$row'); if ($result){ $value['hours'] = $result['hours']; }
Everything is fine, except that I tried almost every combination of declarations for the foreach loop, but I keep getting the error that $value['hours'] is an invalid link. I tried declaring $value[] ... but this does not work either. I do not need to go through $value , so another foreach not needed.
Of course, this is easier than my brain perceives 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']; } }
arrays php mysql
be hollenbeck
source share