找出结果数组中重复值的总数 Codeigniter
我正在尝试查找从 codeigniter 中的活动记录收到的结果中重复值的计数。
For example:
1 => 12
2 => 21
3 => 22
4 => 21
5 => 12
6 => 45
有没有一种方法可以找出总数,从而得出 12 = 1、21 = 2、22 = 1、12 = 2、45 = 1 等?
Codeigniter 方式会很棒,但我也对 PHP 方式持开放态度。
OK array_count_values 不适用于以下数组:
Array
(
[0] => Array
(
[key_id] => 1790
[key_name] => printer brisbane
[link_id] => 1130
[link_url] => 99cards.com
)
[1] => Array
(
[key_id] => 1982
[key_name] => test
[link_id] => 1130
[link_url] => 99cards.com
)
)
有没有办法我们可以找出 link_id 计数为 2?
我自己找到了答案。下面是代码:
$e = 0;
foreach ($q as $qs){
$i = 1;
foreach ($q as $qss){
if($qss['link_id'] == $qs['link_id']){
$q[$e]['link_count'] = $i;
}
$i++;
}
$e++;
}
I'm trying to find count of duplicate values in result received from active record in codeigniter.
For example:
1 => 12
2 => 21
3 => 22
4 => 21
5 => 12
6 => 45
Is there a way we can find out total count so it comes out with 12 = 1, 21 = 2, 22 =1, 12 = 2, 45 = 1 etc?
Codeigniter way would be great but I am also open to PHP way.
OK array_count_values does not work with below array:
Array
(
[0] => Array
(
[key_id] => 1790
[key_name] => printer brisbane
[link_id] => 1130
[link_url] => 99cards.com
)
[1] => Array
(
[key_id] => 1982
[key_name] => test
[link_id] => 1130
[link_url] => 99cards.com
)
)
Is there a way we can find out that link_id count is 2?
I found answer myself. Below is the code:
$e = 0;
foreach ($q as $qs){
$i = 1;
foreach ($q as $qss){
if($qss['link_id'] == $qs['link_id']){
$q[$e]['link_count'] = $i;
}
$i++;
}
$e++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
array_count_values()
专为此类而设计目的它从输入中返回值的关联数组作为键,并将其计数作为值。
上面的例子将输出:
array_count_values()
made for such purposeit Returns an associative array of values from input as keys and their count as value.
The above example will output:
我们可以手动完成,如下所示:
We can do it manually as follows: