title: How to dynamically set array keys in php
body: <p>I have some logic that is being used to sort data but depending on the user input the data is grouped differently.  Right now I have five different functions that contain the same logic but different groupings.  Is there a way to combine these functions and dynamically set a value that will group properly.  Within the function these assignments are happening </p>
  
  <p>For example, sometimes I store the calculations simply by: </p>
  
  <pre><code>$calcs[$meter['UnitType']['name']] = ...
  </code></pre>
  
  <p>but other times need a more specific grouping: </p>
  
  <pre><code>$calcs[$meter['Resource']['name']][$meter['UnitType']['name']][date('Y-m',$start)] =...
  </code></pre>
  
  <p>As you can see sometimes it is stored in a multidiminesional array and other times not.  I have been trying to use eval() but without success (not sure that is the correct approach).  Storing the data in a temporary variable does not really save much because there are many nested loops and if statements so the array would have to be repeated in multiple places. </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I hope the following example explains my problem better.  It is obviously a dumbed down version:</p>
  
  <pre><code>if(){
       $calcs[$meter['UnitType']['name']] = $data;
  } else {
      while () {
         $calcs[$meter['UnitType']['name']] = $data;
      }
  } 
  </code></pre>
  
  <p>Now the same logic can be used but for storing it in different keys:</p>
  
  <pre><code>if(){
       $calcs[$meter['Resource']['name']][$meter['UnitType']['name']][date('Y-m',$start)] = $data;
  } else {
      while () {
         $calcs[$meter['Resource']['name']][$meter['UnitType']['name']][date('Y-m',$start)] = $data;
      }
  } 
  </code></pre>
  
  <p>Is there a way to abstract out the keys in the $calc[] array so that I can have one function instead of having multiple functions with different array keys?</p>