Brian Dunning wrote:
I'm trying to add a number to a value in an array. Pretend I have this:
$new_value = array('orange', 2);
$arr = array(
array('blue', 4),
array('orange', 5),
array('green', 6));
I want to add the new value to the existing matching array element, so I
end up with this:
$arr = array(
array('blue', 4),
array('orange', 7),
array('green', 6));
Seems like it should be really simple but all the ways I can figure out
to do it are too kludgey.
It's rather easy:
for ($i=0; $i<count($arr); $i++) {
if ($arr[$i][0] == $new_array[0]) {
$arr[$i][1] += $new_array[1];
break; // Optional - means the first "orange" found will be
// updated only
}
}
--
Richard Heyes
Employ me:
http://www.phpguru.org/cv
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php