I am trying to export data into an analysis but I’m doing something wrong.
Basically I have a report that has between 1 and 9 subjects.
I am writing this export-tool from scratch, the data is from a legacy system.
The analysis should be exported as CSV, with columns like:
'report_id';'subject0';'subject1'…all the way to 'subject9'
I tried foreach-ing over an array of subjects of a report, and keeping count:
$iterator=0;
foreach ($subjects as $subject)
{
$this->analysis [$report_id] ->subject${iterator} = (string)$subject;
$iterator++;
}
Update
Because I may not use variables on the right side of the arrow operator, I’ve rewritten it as a very un-DRY switch:
$iterator = 0;
foreach ($categories as $subject)
{
switch ($iterator)
{
case 0:
$this->analysis [$report_id] ->subject0 = $subject;
break;
case 1:
$this->analysis [$report_id] ->subject1 = $subject;
break;
case 2:
$this->analysis [$report_id] ->subject2 = $subject;
break;
// I have 6 more cases, just to fill all 9 subjects, snipped for brevity
default:
//some errorhandling //snipped for brevity
break 2;
}
$iterator++;
}
$this->analysis [$report_id] is an object that stores strings as follows:
$this->analysis [$report_id] ->subject0 = "foo"
The $report_id is a result of an earlier loop.
$this->analysis is an array filled with (report)objects that are included in the analysis.
I hope that anyone can find the flaw and point me in the direction of DRY-ing up my implementation. Bonus points for the philosophy behind it, so that I’m not only helped with this problem, but have a better understanding of the oop-php world.
I have no idea where the $report_id come from …
Usually, I will do this :-
make use of variable to represent variable variable
docs :- http://php.net/manual/en/language.variables.variable.php