我有机会在我的代码中处理foreach中的重复计算 声明。重点是如何进行递归?
在这段代码中我总是使用相同的代码,当有“HSPK”时,计算将采用相同的方法(foreach),对于这个例子我在“第五次计算”中表示。
function sum_hspk($hspk_uraian_ssh_hspk_kode) {
$ssh_total = 0;
$ssh_total_2 = 0;
$ssh_total_3 = 0;
$harga_analisa_hspk = 0;
$harga_analisa_satuan = 0;
$hspk_reference = json_decode(get_hspk_reference_uraian_by_hspk_uraian_hspk_kode($hspk_uraian_ssh_hspk_kode));
$hspk_uraian = json_decode(get_hspk_uraian_by_kegiatan($hspk_reference->hspk_kegiatan_kode));
foreach($hspk_uraian as $row) { // first foreach
$hspk_uraian_koefisien = $row->hspk_uraian_koefisien;
if($row->hspk_uraian_ssh_hspk_sumber == "SSH") { // first if
$hspk_uraian_koefisien_ssh = $hspk_uraian_koefisien;
$ssh = json_decode(get_ssh_detail_by_kode($row->hspk_uraian_ssh_hspk_kode));
$ssh_total += ($ssh->ssh_uraian_harga * $hspk_uraian_koefisien_ssh);
} elseif($row->hspk_uraian_ssh_hspk_sumber == "HSPK") {
$hspk_uraian_koefisien_hspk_2 = $hspk_uraian_koefisien;
$hspk_uraian = json_decode(get_hspk_uraian_by_kegiatan($row->hspk_uraian_ssh_hspk_kode));
foreach($hspk_uraian as $row) { // second foreach
$hspk_uraian_koefisien = $row->hspk_uraian_koefisien;
if($row->hspk_uraian_ssh_hspk_sumber == "SSH") { // second if
$hspk_uraian_koefisien_ssh = $hspk_uraian_koefisien;
$ssh = json_decode(get_ssh_detail_by_kode($row->hspk_uraian_ssh_hspk_kode));
$ssh_total_2 += ($ssh->ssh_uraian_harga * $hspk_uraian_koefisien_ssh);
}elseif($row->hspk_uraian_ssh_hspk_sumber == "HSPK") {
$hspk_uraian_koefisien_hspk_3 = $hspk_uraian_koefisien;
$hspk_uraian = json_decode(get_hspk_uraian_by_kegiatan($row->hspk_uraian_ssh_hspk_kode));
foreach($hspk_uraian as $row) { // third foreach
$hspk_uraian_koefisien = $row->hspk_uraian_koefisien;
if($row->hspk_uraian_ssh_hspk_sumber == "SSH") {
$hspk_uraian_koefisien_ssh = $hspk_uraian_koefisien;
$ssh = json_decode(get_ssh_detail_by_kode($row->hspk_uraian_ssh_hspk_kode));
$ssh_total_3 += ($ssh->ssh_uraian_harga * $hspk_uraian_koefisien_ssh);
}else if($row->hspk_uraian_ssh_hspk_sumber == "HSPK") {
$hspk_uraian_koefisien_hspk_4 = $hspk_uraian_koefisien;
$hspk_uraian = json_decode(get_hspk_uraian_by_kegiatan($row->hspk_uraian_ssh_hspk_kode));
foreach ($hspk_uraian as $row) { // fourth foreach
$hspk_uraian_koefisien = $row->hspk_uraian_koefisien;
if($row->hspk_uraian_ssh_hspk_sumber == "SSH") {
$hspk_uraian_koefisien_ssh = $hspk_uraian_koefisien;
$ssh = json_decode(get_ssh_detail_by_kode($row->hspk_uraian_ssh_hspk_kode));
$ssh_total_4 += ($ssh->ssh_uraian_harga * $hspk_uraian_koefisien_ssh);
}elseif($row->hspk_uraian_ssh_hspk_sumber == "HSPK") {
// fifth handling
} // end fourth if
} // end fourth foreach
$harga_analisa_satuan = ((($ssh_total_4) * 0.15) + ($ssh_total_4));
$harga_analisa_hspk = ($harga_analisa_satuan * $hspk_uraian_koefisien_hspk_3);
$ssh_total_3 += $harga_analisa_hspk;
} // end third if
} // end third foreach
$harga_analisa_satuan = ((($ssh_total_3) * 0.15) + ($ssh_total_3));
$harga_analisa_hspk = ($harga_analisa_satuan * $hspk_uraian_koefisien_hspk_3);
$ssh_total_2 += $harga_analisa_hspk;
}; // end second if
} // end second foreach
$harga_analisa_satuan = ((($ssh_total_2) * 0.15) + ($ssh_total_2));
$harga_analisa_hspk = ($harga_analisa_satuan * $hspk_uraian_koefisien_hspk_2);
$ssh_total += $harga_analisa_hspk;
} // end first if
} // end first foreach
$harga_analisa_hspk = ((($ssh_total) * 0.15) + ($ssh_total));
return $harga_analisa_hspk;
} // end function
执行该函数的示例:
$test = sum_hspk('01.04');
echo $test;
答案 0 :(得分:1)
要创建递归函数,您需要考虑基本情况是什么,以及您希望从函数中获取什么(ssh_total)。在这种情况下,sum_hspk从“SSH”计算 ssh_total & “HSPK”。 “SSH”是您的基本案例,您可以直接计算该值。但对于“HSPK”,它依赖于“SSH”和“HSPK”,因此,您需要采用自上而下的方法,直到发现基本情况“SSH”。您需要在“HSPK”条件下调用sum_hspk。
您应该进行此更改以进行递归。
function get_ssh_total($row) {
$ssh_total = 0;
$hspk_uraian_koefisien = $row->hspk_uraian_koefisien;
if ($row->hspk_uraian_ssh_hspk_sumber == "SSH") {
$hspk_uraian_koefisien_ssh = $hspk_uraian_koefisien;
$ssh = json_decode(get_ssh_detail_by_kode($row->hspk_uraian_ssh_hspk_kode));
$ssh_total = ($ssh->ssh_uraian_harga * $hspk_uraian_koefisien_ssh);
}
else if ($row->hspk_uraian_ssh_hspk_sumber == "HSPK") {
$hspk_uraian_koefisien_hspk_2 = $hspk_uraian_koefisien;
$hspk_uraian = json_decode(get_hspk_uraian_by_kegiatan($row->hspk_uraian_ssh_hspk_kode));
foreach($hspk_uraian as $row) {
$ssh_total_2 += get_ssh_total($row);
}
$harga_analisa_satuan = ((($ssh_total_2) * 0.15) + ($ssh_total_2));
$harga_analisa_hspk = ($harga_analisa_satuan * $hspk_uraian_koefisien_hspk_2);
$ssh_total += $harga_analisa_hspk;
}
return $ssh_total;
}
function sum_hspk($hspk_uraian_ssh_hspk_kode) {
$ssh_total = 0;
$ssh_total_2 = 0;
$ssh_total_3 = 0;
$harga_analisa_hspk = 0;
$harga_analisa_satuan = 0;
$hspk_reference = json_decode(get_hspk_reference_uraian_by_hspk_uraian_hspk_kode($hspk_uraian_ssh_hspk_kode));
$hspk_uraian = json_decode(get_hspk_uraian_by_kegiatan($hspk_reference->hspk_kegiatan_kode));
foreach($hspk_uraian as $row) { // first foreach
$ssh_total += get_ssh_total($row);
} // end first foreach
$harga_analisa_hspk = ((($ssh_total) * 0.15) + ($ssh_total));
return $harga_analisa_hspk;
} // end function