doumu5023 2014-12-02 22:06
浏览 34
已采纳

计算1个数组的重复键并更新第二个数组的匹配值

I am trying to count the duplicate keys from array $new_result_months, remove them and update the value of $new_result_users with the matching key. if I would do an array_combine every key should match the value. I really tried but did not managed to think of a version.

$new_result_months = array(  
                        'Dec 2013',
                        'Jan 2014',
                        'Feb 2014',
                        'Mar 2014',
                        'Apr 2014',
                        'May 2014',
                        'Jun 2014',
                        'Jul 2014',
                        'Aug 2014',
                        'Sep 2014',
                        'Oct 2014', 
                        'Oct 2014', // remove one duplicate key and keep unique one
                        'Nov 2014', 
                        'Nov 2014', // remove one duplicate key and keep unique one
                        'Nov 2014', // remove one duplicate key and keep unique one
                        'Dec 2014'
                    );
$new_result_users = array(
                          0,
                          0,
                          0,
                          0,
                          0,
                          0,
                          0,
                          0,
                          0,
                          0,
                          0,
                          1,
                          0,
                          1,
                          1,
                          0
                    );

EXPECTED OUTPUT

$new_result_months = array(  
                        'Dec 2013',
                        'Jan 2014',
                        'Feb 2014',
                        'Mar 2014',
                        'Apr 2014',
                        'May 2014',
                        'Jun 2014',
                        'Jul 2014',
                        'Aug 2014',
                        'Sep 2014',
                        'Oct 2014',
                        'Nov 2014',
                        'Dec 2014'
                    );

$new_result_users = array(
                          0,
                          0,
                          0,
                          0,
                          0,
                          0,
                          0,
                          0,
                          0,
                          0,
                          1,  // needs to match with month oct 2014 and update it to 1
                          2, // needs to match with month nov 2014 and update it to 2
                          0
                    );
  • 写回答

1条回答 默认 最新

  • dqsvf28682 2014-12-02 22:20
    关注

    First you count the duplicated values with array_count_values:

    $aux = array_count_values($new_result_months);
    

    The new created array $aux will have this values:

    ['Dec 2013'] => 1
    ['Jan 2014'] => 1
    ['Feb 2014'] => 1
    ['Mar 2014'] => 1
    ['Apr 2014'] => 1
    ['May 2014'] => 1
    ['Jun 2014'] => 1
    ['Jul 2014'] => 1
    ['Aug 2014'] => 1
    ['Sep 2014'] => 1
    ['Oct 2014'] => 2 
    ['Nov 2014'] => 3
    ['Dec 2014'] => 1
    

    Then, with a foreach loop, you can create the new arrays that you want:

    foreach($aux as $key => $value) {
        $new_result_months[] = $key;
        $new_result_users[] = $value;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c