Adding to a multidimensional array in PHP
Posted
by b. e. hollenbeck
on Stack Overflow
See other posts from Stack Overflow
or by b. e. hollenbeck
Published on 2010-04-20T20:30:15Z
Indexed on
2010/04/20
20:33 UTC
Read the original article
Hit count: 295
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($sql);
if ($result){
$value['hours'] = $result['hours'];
}
I've tried just about every combination of declarations for the foreach loop, but I keep getting the error that it's a non-object. Surely this is easier than my brain is perceiving it.
© Stack Overflow or respective owner