gpt4 book ai didi

php - 在 php foreach 中明智地增加计数器状态

转载 作者:行者123 更新时间:2023-11-29 11:28:59 25 4
gpt4 key购买 nike

我正在开发 cakephp 项目,我必须在其中生成报告。我有两个表,一个表存储客户的值(value),另一个表存储分配给每个用户的潜在客户的值(value)。每个线索都有地位。如果状态为0,我想增加progressleads的计数器,如果status为1,则wonleads的计数器应该增加,依此类推...

我正在连接两个表并获取像这样的数组=>

Array
(
[0] => Array
(
[Customer] => Array
(
[id] => 14
[name] => ABC
)

[Opportunity] => Array
(
[status] => 0
[value] => 50000
)

)

[1] => Array
(
[Customer] => Array
(
[id] => 14
[name] => ABC
)

[Opportunity] => Array
(
[status] => 1
[value] => 10000
)

)

[2] => Array
(
[Customer] => Array
(
[id] => 14
[name] => ABC
)

[Opportunity] => Array
(
[status] => 1
[value] => 7500
)

)

[3] => Array
(
[Customer] => Array
(
[id] => 15
[name] => DEF
)

[Opportunity] => Array
(
[status] => 0
[value] => 45000
)

)

[4] => Array
(
[Customer] => Array
(
[id] => 19
[name] => ST
)

[Opportunity] => Array
(
[status] => 2
[value] => 50000
)

)

[5] => Array
(
[Customer] => Array
(
[id] => 16
[name] => TEST
)

[Opportunity] => Array
(
[status] => 2
[value] => 1000000
)

)

[6] => Array
(
[Customer] => Array
(
[id] => 19
[name] => ST
)

[Opportunity] => Array
(
[status] => 0
[value] => 1000
)

)

[7] => Array
(
[Customer] => Array
(
[id] => 14
[name] => ABC
)

[Opportunity] => Array
(
[status] => 0
[value] =>
)

)

)

从这个数组中,我希望为每个用户提供一条记录,其中包含总潜在客户、正在进行的潜在客户、赢得的潜在客户计数器。我尝试过以下代码:-

$customerdetails = array();
$totalopp = 0;
$progressopp = 0;
$oppval = 0;
$wonopp = 0;
$lostopp = 0;
$billedopp = 0;
$onholdopp = 0;
$newcustid = NULL;
foreach($customer as $k => $val){
$custid = $val["Customer"]["id"];
if($newcustid != $custid){
$oppstatus = $val["Opportunity"]["status"];
$oppval += $val["Opportunity"]["opo_value"];
$totalopp++;
if($oppstatus == 0){
$progressopp++;
}
if($oppstatus == 1){
$wonopp++;
}
if($oppstatus == 2){
$lostopp++;
}
if($oppstatus == 3){
$billedopp++;
}
if($oppstatus == 4){
$onholdopp++;
}
$newcustid = $custid;
}
$customerdetails[$custid]["customername"] = $val["Customer"]["customer_name"];
$customerdetails[$custid]["opportunities"] = $totalopp;
$customerdetails[$custid]["value"] = $oppval;
$customerdetails[$custid]["inprogress"] = $progressopp;
$customerdetails[$custid]["won"] = $wonopp;
$customerdetails[$custid]["lost"] = $lostopp;
$customerdetails[$custid]["billed"] = $billedopp;
$customerdetails[$custid]["onhold"] = $onholdopp;

}

打印 $customerdetails 数组后,我得到以下结果 =>

Array
(
[14] => Array
(
[customername] => ABC
[opportunities] => 6
[value] => 1146000
[inprogress] => 4
[won] => 0
[lost] => 2
[billed] => 0
[onhold] => 0
)

[15] => Array
(
[customername] => DEF
[opportunities] => 2
[value] => 95000
[inprogress] => 2
[won] => 0
[lost] => 0
[billed] => 0
[onhold] => 0
)

[19] => Array
(
[customername] => ST
[opportunities] => 5
[value] => 1146000
[inprogress] => 3
[won] => 0
[lost] => 2
[billed] => 0
[onhold] => 0
)

[16] => Array
(
[customername] => TEST
[opportunities] => 4
[value] => 1145000
[inprogress] => 2
[won] => 0
[lost] => 2
[billed] => 0
[onhold] => 0
)

)

正如您所看到的,只有 4 个线索分配给 ABC,但它显示的机会为 6 个,类似地,其他计数器也显示不正确。谁能帮助我我在这里做错了什么?

最佳答案

我认为您在这里应用了错误的逻辑。 array_column会成功的。请参阅以下代码。

<?php

$customers=array_column($result,'customer');
$opportunity=array_column($result,'opportunity');

$finalResult=array();

foreach($customers as $k=>$customer) {
$opp=$opportunity[$k];
if(!array_key_exists($customer["id"],$finalResult)) {
$finalResult[$customer["id"]]=array();
$finalResult[$customer["id"]]["opportunities"]=0;
$finalResult[$customer["id"]]["value"]=0;
$finalResult[$customer["id"]]["inprogress"]=0;
$finalResult[$customer["id"]]["won"]=0;
$finalResult[$customer["id"]]["lost"]=0;
$finalResult[$customer["id"]]["billed"]=0;
$finalResult[$customer["id"]]["onhold"]=0;
}
$finalResult[$customer["id"]]["customerid"]=$customer["id"];
$finalResult[$customer["id"]]["customername"]=$customer["name"];
$finalResult[$customer["id"]]["opportunities"]++;
$finalResult[$customer["id"]]["value"]+=$opp["value"];
if($opp["status"]==0) {
$finalResult[$customer["id"]]["inprogress"]++;
}
if($opp["status"]==1) {
$finalResult[$customer["id"]]["won"]++;
}
if($opp["status"]==2) {
$finalResult[$customer["id"]]["lost"]++;
}
if($opp["status"]==3) {
$finalResult[$customer["id"]]["billed"]++;
}
if($opp["status"]==4) {
$finalResult[$customer["id"]]["onhold"]++;
}
}

?>

您只需分别获取客户列和机会列,然后遍历客户列即可。

现在,当您遍历时,首先通过将值保存在 $finalResult 数组中来检查您是否已经找到该客户。如果是,则相应地增加计数器,如果不创建数组,则相应地增加计数器。

我认为这会成功。

输出数组可能如下所示:

Array
(
[14] => Array
(
[opportunities] => 4
[value] => 13500
[inprogress] => 2
[won] => 2
[lost] => 0
[billed] => 0
[onhold] => 0
[customerid] => 14
[customername] => ABC
)

[15] => Array
(
[opportunities] => 1
[value] => 45000
[inprogress] => 1
[won] => 0
[lost] => 0
[billed] => 0
[onhold] => 0
[customerid] => 15
[customername] => DEF
)

[19] => Array
(
[opportunities] => 2
[value] => 51000
[inprogress] => 1
[won] => 0
[lost] => 1
[billed] => 0
[onhold] => 0
[customerid] => 19
[customername] => ST
)

[16] => Array
(
[opportunities] => 1
[value] => 100000
[inprogress] => 0
[won] => 0
[lost] => 1
[billed] => 0
[onhold] => 0
[customerid] => 16
[customername] => TEST
)

)

关于php - 在 php foreach 中明智地增加计数器状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37740286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com