I have some logic that is used to sort data, but depending on user input, the data is grouped differently. Right now I have five different functions that contain the same logic, but different groups. Is there a way to combine these functions and dynamically set the value to be grouped correctly. Inside the function, these assignments occur
For example, sometimes I store calculations simply:
$calcs[$meter['UnitType']['name']] = ...
but in other cases a more specific grouping is required:
$calcs[$meter['Resource']['name']][$meter['UnitType']['name']][date('Y-m',$start)] =...
As you can see, sometimes it is stored in a multidimensional array, and in other cases not. I am trying to use eval (), but to no avail (not sure if this is the right approach). Storing data in a temporary variable actually saves little, because there are many nested loops and if statements, so the array must be repeated in several places.
EDIT
Hope the following example will explain my problem better. This is obviously a dead end version:
if(){ $calcs[$meter['UnitType']['name']] = $data; } else { while () { $calcs[$meter['UnitType']['name']] = $data; } }
Now you can use the same logic, but to store it in different keys:
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; } }
Is there a way to abstract the keys in the $ calc [] array to have one function instead of multiple functions with different array keys?
arrays eval php multidimensional-array
Kramer
source share