$data = array();
#Build an array of 'count' per year/week
foreach($mysql_results as $r) {
$data[$r['year']][$r['week']] = $r['count'];
}
#Loop through the $data variable, printing out the 'count' for each year in the array,
#and all 52 weeks that year
for($year = $year_min; $year <= $year_max; $year++) {
for($week=1; $week<=52; $week++) {
echo "$year $week {$data[$year][$week]}";
}
}
Note that year_low and year_high are variables undefined in the current snippet, but they should be known to you.
Also, $mysql_results should be an array containing all rows returned by the database.
In short, the following code does this:
- Make an array grouped per year, then per week, containing the value 'count'
- Loop through this array, displaying, in order, the year, the week, and the value for 'count', if any
(year, week)tuples don't overlap, because access will return the same mutated default hash. The original coder may never have known -- s/he certainly doesn't calldata.keys(), which would be empty, even thoughdata[year]returns something meaningful...h = {}; [[2010, 5, 3], [2010,6,27]].each { |r| h[r[0]] = {} if !h.has_key?(r[0]); h[r[0]][r[1]] = r[2] }data = Hash.new { |me, key| me[key] = {} }.has_key?route. Updated:h = Hash.new { |me, key| me[key] = {} }; [[2010, 5, 3], [2010,6,27]].each { |r| h[r[0]][r[1]] = r[2] }.