duanqinbi9029 2014-09-23 09:50
浏览 35
已采纳

PHP中的自定义数组排序

I have a 5 level nested array and I want to sort it in a custom way.

After some sorting the structure is:

[shortcode1] => Array
    (
        [country1] => Array
            (
                [count] => 10
                [revenue_value] => 11
            )

    )

[shortcode2] => Array
    (
        [country1] => Array
            (
                [count] => 24
                [revenue_value] => 52
            )

    )
[shortcode3] => Array
    (
        [country2] => Array
            (
                [count] => 25
                [revenue_value] => 52
            )

    )

The result I want is to group the array by country and have it like:

[country1] => Array
(
    [count] => 34            // sum of all counts of country1
    [revenue_value] => 63    // sum of all revenue_values of country1
)
[country2] => Array
(
    [count] => 25            // sum of all counts of country2
    [revenue_value] => 52    // sum of all revenue_values of country2
)

This is my code so far:

        <?php foreach ($country_shortcode as $shortcode): 
            $count = 0; 
            $revenue = 0;
            ?>
            <?php foreach ($shortcode as $country=>$value): ?>
                <?php 
                    $count += $value['count'];
                    $revenue += $value['revenue_value'];
                    $country_sum[$country] = array(
                            'count' => $count,
                            'revenue' => $revenue,
                        ); 
                    ?>
            <?php endforeach; ?>
        <?php endforeach; ?>

But it only displays the last value per country

UPDATE + ANSWER:

This is the updated code that does exactly what I wanted to do.

        $country_data;
        <?php foreach ($country_shortcode as $shortcode): 
            $count = 0; 
            $revenue = 0;
            ?>
            <?php foreach ($shortcode as $country=>$value): ?>
                <?php 
                    if (!isset($country_sum[$country])) $country_sum[$country] = array('count' => 0, 'revenue'=>0);
                    $country_sum[$country]['count'] += $value['count'];
                    $country_sum[$country]['revenue'] += $value['revenue_value'];
                ?>
            <?php endforeach; ?>
        <?php endforeach; ?>
  • 写回答

1条回答 默认 最新

  • donglin1192 2014-09-23 09:53
    关注

    It's too easy... just loop over the array creating a new one

    foreach ($array as $sub) {
      foreach ($sub as $countryName => $data) {
        if (!isset($result[$countryName])) {
           $result[$countryName] = ['count' => 0, 'revenue_value' => 0];
        }
        $result[$countryName]['count'] += $data['count'];
        $result[$countryName]['revenue_value'] += $data['revenue_value'];
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目