首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我想要现有数组中的新数组,关键字作为旧数组中的重复值,值作为重复计数?

我想要现有数组中的新数组,关键字作为旧数组中的重复值,值作为重复计数?
EN

Stack Overflow用户
提问于 2017-02-22 18:57:13
回答 5查看 198关注 0票数 0

我的旧数组

array:7 [
  0 => "22-Feb-2017"
  1 => "22-Feb-2017"
  2 => "22-Feb-2017"
  3 => "27-May-2015"
  4 => "10-May-2015"
  5 => "10-May-2015"
  6 => "08-May-2015"
]

我想要新的数组,因为

array:7 [
  22-Feb-2017 => "3"
  27-May-2015 => "1"
  10-May-2015 => "2"
  08-May-2015 => "1"
]
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2017-02-22 19:05:34

不确定您是否在寻找这样的东西

http://php.net/manual/en/function.array-count-values.php

票数 5
EN

Stack Overflow用户

发布于 2017-02-22 19:07:52

请检查以下输出:

 $a=array(0 => "22-Feb-2017",
    1 => "22-Feb-2017",
    2 => "22-Feb-2017",
    3 => "27-May-2015",
    4 => "10-May-2015",
    5 => "10-May-2015",
    6 => "08-May-2015");

    ///array_count_values counts the same values count////
    $new_array = array_count_values($a);
/// now let's interchange the keys and values////
    foreach($new_array as $key=>$value){
        $out[$value] = $key;
    }

    print_r($out);
票数 2
EN

Stack Overflow用户

发布于 2017-02-22 19:09:08

一种方法是:

$dates = [
    0 => "22-Feb-2017",
    1 => "22-Feb-2017",
    2 => "22-Feb-2017",
    3 => "27-May-2015",
    4 => "10-May-2015",
    5 => "10-May-2015",
    6 => "08-May-2015",
];

$result = [];

foreach ($dates as $date) {
    $result[$date] = array_key_exists($date, $result) ? $result[$date] + 1 : 1;
}

print_r($result);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42389277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档