title: How can I refactor this hideous beast?
body: <p>I have a query that I execute, then I use </p>
  
  <pre><code>while($row = $result-&gt;fetch_assoc()){
          $data[] = $row;
  </code></pre>
  
  <p>I know this is grotesque, but I'm unsure how to make this an object or a multidimensional array so I just made something like this..</p>
  
  <pre><code>$NUH2016 = array();
  $NUH2017 = array();
  $NUH2018 = array();
  $NUH2019 = array();
  $NUH2020 = array();
  $NUH2021 = array();
  $RBN2016 = array();
  $RBN2017 = array(); ...
  
  ...$GDT2019 = array();
  $GDT2020 = array();
  $GDT2021 = array();
  
  while($row = $result-&gt;fetch_assoc()){
          $data[] = $row;
          if($row['Location'] == 'NUH' &amp;&amp; $row['Year'] == '2016'){
              $NUH2016[] = $row['P_1'];
              $NUH2016[] = $row['P_2'];
              $NUH2016[] = $row['P_3'];
              $NUH2016[] = $row['P_4'];
          }
          if($row['Location'] == 'NUH' &amp;&amp; $row['Year'] == '2017'){
              $NUH2017[] = $row['P_1'];
              $NUH2017[] = $row['P_2'];
              $NUH2017[] = $row['P_3'];
              $NUH2017[] = $row['P_4'];
          }
          //...for all locations and years
  
   ...then
   $content = array();
  $results = array();
  
  $NUH2016_total = array_sum($NUH2016);
  $NUH2017_total = array_sum($NUH2017);
  $NUH2018_total = array_sum($NUH2018);
  $NUH2019_total = array_sum($NUH2019);
  $NUH2020_total = array_sum($NUH2020);
  $NUH2021_total = array_sum($NUH2021);
  
  
  $results['NUH~2016'] = $NUH2016_total;
  $results['NUH~2017'] = $NUH2017_total;
  $results['NUH~2018'] = $NUH2018_total;
  $results['NUH~2019'] = $NUH2019_total;
  $results['NUH~2020'] = $NUH2020_total;
  $results['NUH~2021'] = $NUH2021_total;
  
  $RBN2016_total = array_sum($RBN2016);
  $RBN2017_total = array_sum($RBN2017);
  $RBN2018_total = array_sum($RBN2018);
  $RBN2019_total = array_sum($RBN2019);
  $RBN2020_total = array_sum($RBN2020);
  $RBN2021_total = array_sum($RBN2021);
  
  
  $results['RBN~2016'] = $RBN2016_total;
  $results['RBN~2017'] = $RBN2017_total;
  $results['RBN~2018'] = $RBN2018_total;
  $results['RBN~2019'] = $RBN2019_total;
  $results['RBN~2020'] = $RBN2020_total;
  $results['RBN~2021'] = $RBN2021_total;
  ...etc
  
  $content['Results'] = $results;
  $response = json_encode($content);
  echo $response;
  </code></pre>
  
  <p>So what I want to do is make an object or associative array that looks like this.</p>
  
  <pre><code>content {
          produced hours{   
                         nuh{ 2016:16,000
                              2017:8,000
                              ...
                             }
                         rbn{ 2016:9,000
                              2017:whatever
                              ....
                        }
             man hours{
                         nuh{ 2016:4,000
                              2017:2,000
                              ...
                             }
                         rbn{ 2016:1,000
                              2017:more stuff
                              ....
                        }
  </code></pre>
  
  <p>but I'm pretty new to php objects. How can I refactor it so that I don't have to initialize every single empty array, have a bunch of if statements to make each entry from the query fit into an array to be summed, and then put it all into an object at the end to get back from an ajax success function?</p>