在两个foreach循环中组合xml结果

时间:2015-10-21 08:51:48

标签: php loops foreach

我的结果来自已转换为XML的SoapClient。从那里我有两个foreach循环,一个用于数据,另一个用于显示使用数据的代理。由于不同类型的条目名称,日期表对于每个代理具有多个条目,例如销售 - 气体或电力销售。我想要做的是结合计数和通话持续时间,因此这些结果中只有一个代理。

以下是代码:

$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

Gareth Wright 17 04:11:23

Gareth Wright 5 00:46:13

Chloe Delgado 82 20:42:26

Chloe Delgado 16 04:07:49

Chloe Delgado 3 00:43:58

Sophie Berrill 5 01:35:52

Sophie Berrill 1 00:19:38

Tom Pearson 84 00:56:40

Tom Pearson 15 04:10:46

Tom Pearson 3 01:03:23

Sean Lee 93 03:00:09

Sean Lee 12 03:10:12

Sean Lee 2 00:00:00

1 个答案:

答案 0 :(得分:1)

首先,您必须构建一个这样的数组:

$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...
    )

    [...] => ...

)