我有一个从SoapClient获得的结果,该结果已转换为XML。因此,我有两个foreach循环,一个用于数据,另一个用于显示使用数据的代理。由于条目名称的类型不同(例如,SALE-GAS或SALE ELECTRIC),该日期表中每个代理商都有多个条目。我想要做的是结合计数和通话时间,因此这些结果中只有一位特工。
这是代码:
$xmlArray = xml2array($xml);
if( isset($xmlArray["data"])
&& isset($xmlArray["data"]["Report.Disposition.Summary3"])
&& isset($xmlArray["data"]["Report.Disposition.Summary3"]["row"]) )
{
$rows = $xmlArray["data"]["Report.Disposition.Summary3"]["row"];
if( count($rows) > 0 )
{
foreach($rows as $row)
{
//$userDataArray = $row["@attributes"];
//var_export($userDataArray);
$UserId = $row["@attributes"]['UserId'];
$Count = $row["@attributes"]['Count'];
$Code = $row["@attributes"]['Code'];
$TalkDuration = $row["@attributes"]['TalkDuration'];
$company = $_SESSION['user']['company']; $sth = $db->prepare("SELECT id, firstname, lastname FROM users WHERE company = '".$company."' AND agent_id = '".$UserId."'");
$sth->execute();
$agents = $sth->fetchAll();
foreach($agents as $name){
if(($Code == "1327 SALE - DUAL FUEL SWITCH") || ($Code == "1328 SALE - ELECTRICITY SWITCH") || ($Code == "1329 SALE - GAS SWITCH") || ($Code == "122 CLAIM STARTED BANK REFUNDS") || ($Code == "12202 CLAIM STARTED PBA")){
echo '<tr>';
echo '<td>'.$name['firstname'].' '.$name['lastname'].'</td>';
echo '<td>'.$Count.'</td>';
echo '<td>'.gmdate("H:i:s", $TalkDuration).'</td>';
echo '</tr>';
}
}
}
}
}
这是它产生的结果:
Gareth Wright 77 21:27:36
加雷斯·赖特17 04:11:23
加雷斯·赖特5 00:46:13
克洛伊·德尔加多(Chloe Delgado)82 20:42:26
克洛伊·德尔加多(Chloe Delgado)16 04:07:49
克洛伊·德尔加多(Chloe Delgado)3 00:43:58
索菲·贝里尔5:35:52
索菲·贝瑞尔1 00:19:38
汤姆·皮尔森84 00:56:40
汤姆·皮尔森15 04:10:46
汤姆·皮尔森3 01:03:23
肖恩·李93 03:00:09
肖恩·李12 03:10:12
肖恩·李2 00:00:00
首先,您必须构建一个像这样的数组:
$arr = array(
'0' => array(
'firstname' => "Gareth",
'lastname' => "Wright",
'count' => 77,
'talkDuration' => 'a date...'
),
'1' => array(
'firstname' => "Gareth",
'lastname' => "Wright",
'count' => 17,
'talkDuration' => 'a other date...'
),
'2' => array(
'...' => '...'
)
);
如何 ?像这样:
$arr = array();
foreach($rows as $row){
//some process like getting $agent and $count, ...
foreach($agents as $name){
$arr[] = array(
'firstname' => $name['firstname'],
'lastname' => $name['lastname'],
'count' => $count,
'talkDuration' => $TalkDuration
);
}
}
因此,在两个foreach之后,运行以下命令:
$result = array();
foreach ($arr as $data) {
$name = $data['firstname']."_".$data['lastname'];
if (isset($result[$name])) {
$result[$name]['count'] = $result[$name]['count']+$data['count'];
$result[$name]['talkDuration'] = $result[$name]['talkDuration']+$data['talkDuration'];
} else {
$result[$name] = $data;
}
}
输出:
print_r($result);
=================
Array
(
[Gareth_Wright] => Array
(
[firstname] => Gareth
[lastname] => Wright
[count] => 94
[talkDuration] => a date...
)
[...] => ...
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句