PHP –在foreach循环中对具有相同ID的输出分组

时间:2018-09-12 02:47:14

标签: php arrays multidimensional-array

我有一个用户通过自定义文件上传字段(高级自定义字段)从Wordpress后端上传.csv文件。然后,我调用此字段以从该文件中检索数据以创建所述数据的数组:

$upload_cq = get_field('report_current_quarter');
$report_cq = array();
if(($handle_cq = fopen($upload_cq, "r")) !== FALSE)
{
    while(($data_cq = fgetcsv($handle_cq, 1000, ",")) !== FALSE)
    {
        $report_cq[] = $data_cq;
    }
    $results_cq = array_slice($report_cq, 3); // remove unwanted row
    fclose($handle_cq);
}

创建数组后,我将创建另一个具有键和关联值的数组,并确保“ team_id”列存在且不为空(因此,有一个teamName函数,我不会进入):

foreach($results_cq as $results)
{
    $team_id = $results[6];
    if(!empty($team_id))
    {
        $team_totals_cq[] = array(
            'id' => $team_id,
            'team' => teamName($team_id),
            'total_volume' => $results[41],
            'total_closed' => $results[24],
            'listings_closed' => $results[22],
            'buyers_closed' => $results[23],
            'total_agc' => $results[29],
            'rental_agc' => $results[30],
        );
    }
}
echo '<pre>'; print_r($team_totals_cq); echo '</pre>';

打印阵列时,得到以下信息。我的问题是;如何将结果与同一个团队“ id”分组并加总结果(结果= total_volume,total_closed,listings_closed,buyers_closed,total_agc和rental_agc):

Array
(
    ...

    [6] => Array
        (
            [id] => 0011
            [team] => Williamson Team
            [total_volume] => $990,000
            [total_closed] => 4
            [listings_closed] => $0.00
            [buyers_closed] => 1
            [total_agc] => $20,812.50
            [rental_agc] => $23,812.50
        )

    ...

    [9] => Array
        (
            [id] => 0011
            [team] => Williamson Team
            [total_volume] => $415,000
            [total_closed] => 2
            [listings_closed] => $0.00
            [buyers_closed] => 0
            [total_agc] => $12,450.00
            [rental_agc] => $12,450.00
    )

    ...
)

我已经尽力尝试了一切,但没有任何效果。

1 个答案:

答案 0 :(得分:0)

一种方法是使用team_id作为$team_totals_cq中的键,然后在循环中简单地累加值。

(注意:preg_replace函数会剥离所有非数字或。)

$team_totals_cq = [];

foreach ($results_cq as $results) {
    $team_id = $results[6];

    if (!empty($team_id)) {
        if (!isset($team_totals_cq[$team_id])) {
            $team_totals_cq[$team_id] = [
                'id' => $team_id,
                'team' => teamName($team_id),
                'total_volume' => preg_replace("/[^0-9\.]/", '', $results[41]),
                'total_closed' => $results[24],
                'listings_closed' => preg_replace("/[^0-9\.]/", '', $results[22]),
                'buyers_closed' => $results[23],
                'total_agc' => preg_replace("/[^0-9\.]/", '', $results[29]),
                'rental_agc' => preg_replace("/[^0-9\.]/", '', $results[30])
            ];
        } else {
            $team_totals_cq[$team_id]['total_volume'] += preg_replace("/[^0-9\.]/", '', $results[41]);
            $team_totals_cq[$team_id]['total_closed'] += $results[24];
            $team_totals_cq[$team_id]['listings_closed'] += preg_replace("/[^0-9\.]/", '', $results[22]); 
            $team_totals_cq[$team_id]['buyers_closed'] += $result[23];
            $team_totals_cq[$team_id]['total_agc'] += preg_replace("/[^0-9\.]/", '', $results[29]);
            $team_totals_cq[$team_id]['rental_agc'] += preg_replace("/[^0-9\.]/", '', $results[30]);
        }
    }
}