【问题标题】:How to count value inside array? [duplicate]如何计算数组内的值? [复制]
【发布时间】:2026-02-06 15:10:02
【问题描述】:

我现在在一个列表中有多个值我想计算在这个列表中出现了多少次 100(使用 PHP)。

我的 PHP 代码:

<table class="widefat" style="margin-top:50px;">

输出:

1      33,55,88,100,66,4
       45,45,58,49,49,4
       100,100,50,49,80,4


2      33,55,88,100,66,4
       45,45,58,49,49,4
       100,100,50,49,80,4


3      33,55,88,100,66,4
       45,45,58,49,49,4
       100,100,50,49,80,4

我希望输出是:

1      100 (1) Times
       100 (0) Times
       100 (2) Times


2      100 (1) Times
       100 (0) Times
       100 (2) Times

3      100 (1) Times
       100 (0) Times
       100 (2) Times

【问题讨论】:

  • 我有一个变量 $a 当我回显这个时,他们会打印这个值。回声 $a;

标签: php arrays count


【解决方案1】:

您可以使用explodearray_maparray_filterarray_count_values 的组合来用逗号分隔值并保留值为 100 的值。

例如:

$items = [
    1 => [
        "33,55,88,100,66,4",
        "45,45,58,49,49,4",
        "100,100,50,49,80,4"
    ],
    2 => [
        "33,55,88,100,66,4",
        "45,45,58,49,49,4",
        "100,100,50,49,80,4"
    ],
    3 => [
        "33,55,88,100,66,4",
        "45,45,58,49,49,4",
        "100,100,50,49,80,4"
    ],
];

foreach ($items as &$item) {
    $item = array_map(function ($x) {
        $res = array_filter(array_count_values(
            explode(',', $x)
        ), function ($key) {
            return $key === 100;
        }, ARRAY_FILTER_USE_KEY);
        return sprintf("100 (%s) Times", count($res) === 1 ? $res[100] : 0);
    }, $item);
}

print_r($items);

结果

Array
(
    [1] => Array
        (
            [0] => 100 (1) Times
            [1] => 100 (0) Times
            [2] => 100 (2) Times
        )

    [2] => Array
        (
            [0] => 100 (1) Times
            [1] => 100 (0) Times
            [2] => 100 (2) Times
        )

    [3] => Array
        (
            [0] => 100 (1) Times
            [1] => 100 (0) Times
            [2] => 100 (2) Times
        )

)

Php demo

【讨论】:

    【解决方案2】:

    您可以使用 array_count_values( $array ) 来计算另一个数组上的所有值以及结果。

    示例:

    $count = array_count_values( $array );
    
    echo "$a comes " . $count[$a] . ' times' ;
    

    如果您只想知道一个值,只需执行 if。

    示例:

    $count = array_count_values( $array );
    
    if ( $a == 100 )
      echo "$a comes " . $count[$a] . ' times' ;
    

    编辑:

    更多解释:

    这是一个你要计算值的数组。

    $array = array(12,14,12,100,5,100);
    

    然后使用 array_count_values( ) 获取另一个数组上的值。

    $count = array_count_values( $array );
    

    现在您可以执行一个循环来获取所有值。

    foreach( $array as $a ){
    
      echo "$a comes " . $count[$a] . " times <br/>" ;
    
    }
    

    它将打印:

    12 comes 2 times
    14 comes 1 times
    12 comes 2 times
    100 comes 2 times
    5 comes 1 times
    100 comes 2 times
    

    您可以将 $count[$a] 更改为 $count[100](在循环外)以计算 100 在数组中的次数。

    echo "100 comes " . $count[100] . " times" ;
    

    将打印

    100 comes 2 times
    

    您可以在此处获取更多信息。 https://www.php.net/manual/es/function.array-count-values.php

    【讨论】:

    • 数组 ( [0] => 0 ) 数组 ( [0] => 33 ) 数组 ( [0] => 44 ) 数组 ( [0] => 100 ) 数组 ( [0] = > 33 ) 数组 ( [0] => 66 ) 数组 ( [0] => 100 ) 数组 ( [0] => 12 );如何使用这个数组计算 100?
    • @RohitYaduvanshi 我更新了答案,希望对你有所帮助。