Requirement: Statistics of daily data based on start and end time
The current data retrieved from the database has about 10 two-dimensional arrays with the same structure as follows:
<code>[ '0' => [ 'time' => '2016-8-3', 'data1'=> 'xxx', ... ] ]</code>
Because each array has required fields, these 10 two-dimensional arrays need to be combined. The idea is as follows:
Traverse and retrieve data based on date
<code>while (strtotime($start_time) < strtotime($end_time)) { // 10个foreach foreach($arr as $k => $v) { if (strtotime($start_time) == strtotime($v['time'])) { $data[] = $v[]; ... } } foreach($arr as $k => $v) { if (strtotime($start_time) == strtotime($v['time'])) { $data[] = $v[]; ... } } foreach($arr as $k => $v) { if (strtotime($start_time) == strtotime($v['time'])) { $data[] = $v[]; ... } } ... $start_time = strtotime($start_time . ' +1 day'); }</code>
The final data structure I want to combine:
<code>[ '2016-8-3' => [ 'data' => '', ... ], '2016-8-4' => [ 'data' => '', ... ] .. ]</code>
But it seems that the performance of that while is very poor, and the data cannot be run out. After waiting for a long time, it directly prompts:
Maximum execution time of 30 seconds exceeded
Do you have any good suggestions?
Requirement: Statistics of daily data based on start and end time
The current data retrieved from the database has about 10 two-dimensional arrays with the same structure as follows:
<code>[ '0' => [ 'time' => '2016-8-3', 'data1'=> 'xxx', ... ] ]</code>
Because each array has required fields, these 10 two-dimensional arrays need to be combined. The idea is as follows:
Traverse and retrieve data based on date
<code>while (strtotime($start_time) < strtotime($end_time)) { // 10个foreach foreach($arr as $k => $v) { if (strtotime($start_time) == strtotime($v['time'])) { $data[] = $v[]; ... } } foreach($arr as $k => $v) { if (strtotime($start_time) == strtotime($v['time'])) { $data[] = $v[]; ... } } foreach($arr as $k => $v) { if (strtotime($start_time) == strtotime($v['time'])) { $data[] = $v[]; ... } } ... $start_time = strtotime($start_time . ' +1 day'); }</code>
The final data structure I want to combine:
<code>[ '2016-8-3' => [ 'data' => '', ... ], '2016-8-4' => [ 'data' => '', ... ] .. ]</code>
But it seems that the performance of that while is very poor, and the data cannot be run out. After waiting for a long time, it directly prompts:
Maximum execution time of 30 seconds exceeded
Do you have any good suggestions?
There is something wrong with your loop, causing an endless loop. You can print out the $start_time and $end_time of each cycle. It must be strtotime($start_time) < strtotime($end_time)
It will always be true