tags:

views:

39

answers:

2

Out of this:

$arr = array(
             array('boo', 4),
             array('boo', 1),
             array('foo', 2),
             array('foo', 6)
            );

how best calculate into this?:

$arr = array(
             'boo' => 5,
             'foo' => 8
            );
+3  A: 
$sum = array();
for ( $i = 0; $i < count( $arr ); $i++ )
{
    if ( !isset( $sum[ $arr[$i][0] ] )
        $sum[ $arr[$i][0] ] = 0;
    $sum[ $arr[$i][0] ] += $arr[$i][1];
}

print_r( $sum );
poke
A: 
$arr = array(
         array('boo', 4),
         array('boo', 1),
         array('foo', 2),
         array('foo', 6)
);

Then:

$arr2 = array();
foreach($arr as $value) {
    if(isSet($arr2[$value[0]])) $arr2[$value[0]] += $value[1];
    else $arr2[$value[0]] = $value[1];
}
Tomas Jancik
Format your code by indenting it with four spaces.
BoltClock