Re: Awkward time processing

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Il Thu, 02 Aug 2012 09:25:40 -0700, Robert Williams ha scritto:

> $times = array(
>         17 => '15:31',
>         16 => '15:32',
>         27 => '15:33',
>         14 => '15:34',
>         11 => '15:35',
>         27 => '16:33',
>         14 => '17:34',
>         11 => '11:35',
>         11 => '11:36',
> );

This will not work as expected. Try:

var_dump($times);

You lose 4 array elements.

Better:

$times = array(
 array('time'=>'15:31', 'c':17),
 array('time'=>'15:32', 'c':16),
 array('time'=>'15:33', 'c':27),
 array('time'=>'15:34', 'c':14),
 array('time'=>'15:35', 'c':11),
 array('time'=>'16:33', 'c':27),
 array('time'=>'17:34', 'c':14),
 array('time'=>'11:35', 'c':11),
 array('time'=>'11:36', 'c':11)
);

$result = array();

foreach ($times as $time) {
 if (!array_key_exists($time['time'], $result) {
  $result[$time['time']] = 0;
 }
 $result[$time['time']] += $time['c'];
}


Bye.



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux