Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You want to make it easy for yourself (and potentially other users) to understand what your code is doing, and what you are thinking when you wrote it. Imagine asking for help, imagine outputting debug code or imagine returning to fix a bug 12 months after you last touched the code. Which will be the best and fastest for you/others to understand?</p> <p>If your code requires that you "For each year, display the data" then the first is more logical. If your thinking is "I need to gather all the measures together, then I'll process those" then go for the second option. If you need to re-order by year, then go the first.</p> <p>Based on your example above, though the way I'd handle the above is probably:</p> <pre><code>$array[$year][$month] = $measure; </code></pre> <p>You don't need a specific "measure" element. Or if you do have two elements per month:</p> <pre><code>$array[$year][$month] = array('measure' =&gt; $measure, 'value'=&gt;$value); or $array[$year][$month]['measure'] = $measure; $array[$year][$month]['value'] = $value; </code></pre> <p>Then you can go:</p> <pre><code>for($year = $minYear; $year &lt;= $maxYear; $year++) { // Or "foreach" if consecutive for ($month = 1; $month &lt;= 12; $month++) { if (isset($array[$year][$month])) { echo $array[$year][$month]['measure']; // You can also check these exist using isset if required echo $array[$year][$month]['value']; } else { echo 'No value specified' } } } </code></pre> <p>Hope that helps with your thinking. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload