最近我遇到了一些有关嵌套foreach循环的恼人问题。
有人能指出我正确的方向还是给我一些提示如何正确摆脱嵌套的foreach循环?到目前为止,我已经将嵌套的foreach转移到了Doctor类中,并在那里进行了所有诊断迭代......但似乎并不是理想的方法......我在这里缺少一些基本上不为deing模式设计的东西?
Class Treatment {
/**
* get Diagnosis and releated Doctors information (this is an temporary approach)
*/
public function getDiagnosis(){
$diagnosisContainer = [];
foreach($this->doctorIterator as $key => $doc){ //iterate all doctors
//how to get rid of this nested foreach ?
foreach($doc->getCurrentDiagnosis() as $diagnosis) { //iterate all diagnosis
$std = new \StdClass();
$std->docType = $doc->getType();
$std->docLastname = $doc->getLastname();
$std->docFirstname = $doc->getFirstname();
$std->diagnosis = $diagnosis;
$diagnosisContainer = array_merge([$std], $diagnosisContainer);
}
}
return $diagnosisContainer;
} }
到目前为止,这是我提出的解决方案,其中迭代器仅移入Doctor类,但对我来说通常是相同的。
public function getDiagnosis($type = 0){
$diagnosisContainer = [];
foreach($this->doctorIterator as $key => $doc){ //iterate all doctors
$diagnosisContainer = array_merge($diagnosisContainer, $doc->getDiagnosisWithDoctor()); }