0

I have a large array in PHP, having near around 168000 keys and values. There is date (Y-m-d) and hour in key and numeric value in value. So value is just a numeric. And key is in Y-m-d_H format. Array looks like following:

$input = array('2008-01-01_00' => 123, '2008-01-01_01' => 456, ...... , '2012-09-22_16' => 789);

I need to find the total of last month, last year, current year, current month and etc. Which is the best way to find it? Please suggest.

  • Why don't you use a database for such tasks? – Sirko Sep 22 '12 at 10:03
  • @Sirko MYSQL database is little complex here, it uses 240 database * 750 tables. So I need to achieve this on PHP coding level. Please suggest. –  Sep 22 '12 at 10:06
  • 2
    With the same queries, you currently use to pull the data, you could create a temporary table and do all your reporting by simple aggregations ... – Sirko Sep 22 '12 at 10:07
  • @Sirko I think the way you have suggested, temp table, will take more execution time. As it will insert 168000 records to that table then it will process it. What do you say? –  Sep 22 '12 at 10:09
  • It would be less time consuming than pulling all data from PHP and doing the calculations there manually. – Sirko Sep 22 '12 at 10:37
  • why such a large array in PHP? – user1122069 Sep 22 '12 at 16:44

3 Answers3

0

What about this:

$results=array();

foreach ($input as $k=>$v) {

  $date=explode('_',$k);
  $date=explode('-',$date[0]);

  //Store year
  $key=$date[0];
  if (!isset($results[$key])) $results[$key]=0;
  $results[$key]+=$v;

  //Store month
  $key.='-'.$date[1];
  if (!isset($results[$key])) $results[$key]=0;
  $results[$key]+=$v;

  //Store day
  $key.='-'.$date[2];
  if (!isset($results[$key])) $results[$key]=0;
  $results[$key]+=$v;
}

print_r($results);
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
0

Change the keys to ints, e.g.

'2008-01-01_00' -> 2008010100

sort your array with ksort()

Then you can use quicksearch to find something between 2008010100 and 2009010100.

Also, Fastest is to traverse the array once and calculate all the statistics you need.

Antti Rytsölä
  • 1,485
  • 14
  • 24
0

Break it down into manageable execution units and thread them ...

http://pthreads.org/

Joe Watkins
  • 17,032
  • 5
  • 41
  • 62