作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请帮助我使用 php.Consider 过滤掉数组中的重复值,
$arr1 = array('php','jsp','asp','php','asp')
这里我宁愿只打印
array('php'=>2,
'asp'=>2)
试过
print_r(array_count_values($arr1));
但是,它获取每个元素的计数。
最佳答案
好的,在评论和重读你的问题之后,我明白你的意思了。使用 array_count_values()
就差不多了:
$arr1 = array('php','jsp','asp','php','asp');
$counts = array_count_values($arr1);
您只需删除显示为仅出现一次的条目:
foreach ($counts as $key => $val) {
if ($val == 1) {
unset($counts[$key]);
}
}
编辑:不想循环?使用 array_filter()
代替:
// PHP 5.3+ only
$counts = array_filter($counts, function($x) { return $x > 1; });
// Older versions of PHP
$counts = array_filter($counts, create_function('$x', 'return $x > 1;'));
关于php - 使用php过滤掉数组中的重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3583558/
我正在编写一个快速的 preg_replace 来从 CSS 中删除注释。 CSS 注释通常有这样的语法: /* Development Classes*/ /* Un-comment me for
使用 MySQL,我有三个表: 项目: ID name 1 "birthday party" 2 "soccer match" 3 "wine tasting evening" 4
我是一名优秀的程序员,十分优秀!