找出结果数组中重复值的总数 Codeigniter

发布于 2024-12-11 14:32:27 字数 1114 浏览 0 评论 0原文

我正在尝试查找从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星軌x 2024-12-18 14:32:27

array_count_values() 专为此类而设计目的

它从输入中返回值的关联数组作为键,并将其计数作为值。

$array = array(1, "hello", 1, "world", "hello"); 
print_r(array_count_values($array)); 

上面的例子将输出:

Array (
    [1] => 2
    [hello] => 2
    [world] => 1 )

array_count_values() made for such purpose

it Returns an associative array of values from input as keys and their count as value.

$array = array(1, "hello", 1, "world", "hello"); 
print_r(array_count_values($array)); 

The above example will output:

Array (
    [1] => 2
    [hello] => 2
    [world] => 1 )
空城之時有危險 2024-12-18 14:32:27

我们可以手动完成,如下所示:

$count_array = array();
foreach($results as $result)
{
    $link_id = $result['link_id'];
    if(isset($count_array[$link_id]))
    {
        $count_array[$link_id] = $count_array[$link_id]+1;
    }
    else
    {
        $count_array[$link_id] = 1;
    }
}

We can do it manually as follows:

$count_array = array();
foreach($results as $result)
{
    $link_id = $result['link_id'];
    if(isset($count_array[$link_id]))
    {
        $count_array[$link_id] = $count_array[$link_id]+1;
    }
    else
    {
        $count_array[$link_id] = 1;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文