我的旧数组
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"
]
发布于 2017-02-22 19:05:34
不确定您是否在寻找这样的东西
发布于 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);
发布于 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);
https://stackoverflow.com/questions/42389277
复制相似问题