【问题标题】:PHP – Group output with same ID from within foreach loopPHP – 在 foreach 循环中对具有相同 ID 的输出进行分组
【发布时间】:2018-09-12 02:47:14
【问题描述】:

我有一个用户通过自定义文件上传字段(高级自定义字段)从 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 和 rent_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
    )

    ...
)

我在力所能及的范围内尝试了所有方法,但没有任何效果。

【问题讨论】:

标签: php arrays multidimensional-array


【解决方案1】:

一种方法是使用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]);
        }
    }
}

【讨论】:

  • 在字符类中是什么意思?
  • 啊,好吧,我不确定我是否将+ 放在字符类之后。是的,我不会在这样的答案中测试代码,更多的是展示它的工作原理。
  • str_replace(['$', ','], '', '$formatted_string'); 在这种情况下会更轻松。这些值具有可预测的格式,因此正则表达式是多余的。
猜你喜欢
  • 2023-03-28
  • 1970-01-01
  • 2013-09-29
  • 1970-01-01
  • 1970-01-01
  • 2019-07-12
  • 2010-12-13
  • 2018-09-09
  • 1970-01-01
相关资源
最近更新 更多