如何在php数组中计算不同的键和相关的不同值

时间:2017-01-21 13:17:58

标签: php arrays

我试图在下面的例子中计算每个部分的缺席次数和出席人数。但是我不容易计算每个部分的缺席和出席数量可以帮助我任何一个....感谢预先

<?php
Array(
    [0] => Array([Section] => Attendance) 
    [1] => Array([CSE - A] => PRESENT) 
    [2] => Array([CSE - G] => ABSENT) 
    [3] => Array([CSE - A] => ABSENT) 
    [4] => Array([CSE - C] => PRESENT) 
    [5] => Array([CSE - C] => PRESENT) 
    [6] => Array([CSE - C] => PRESENT) 
    [7] => Array([CSE - C] => PRESENT) 
    [8] => Array([IT] => PRESENT) 
    [9] => Array([CSE - D] => ABSENT) 
    [10] => Array([CSE - G] => ABSENT) 
    [11] => Array([CSE - B] => PRESENT) 
    [12] => Array([CSE - A] => ABSENT) 
    [13] => Array([CSE - C] => PRESENT) 
    [14] => Array([CSE - E] => ABSENT) 
    [15] => Array([CSE - B] => ABSENT) 
    [16] => Array([CSE - E] => ABSENT) 
    [17] => Array([CSE - F] => ABSENT) 
    [18] => Array([CSE - G] => ABSENT) 
    [19] => Array([CSE - G] => PRESENT) 
    [20] => Array([CSE - A] => ABSENT) 
    [21] => Array([CSE - D] => PRESENT) 
)

3 个答案:

答案 0 :(得分:0)

$table是您的数据。你必须遍历每个元素。然后,对于每个新密钥,您在使用$result值初始化的0数组中创建数据。如果该值存在,则添加1

//Result initialization
$result = [];
//Loop over each element
foreach($table as $ligne){
    //foreach each element I catch key and its value
    foreach ($ligne as $key => $value){
        //initialization to zero if necessary              
        if (!array_key_exists($key, $result)) $result[$key] = 0;
        if ($value == 'PRESENT'){
            //If present I add 1
            $result[$key] += 1;
        }
    }
}
var_dump($result);

这是一个带有数组第一个值的示例结果:

array (size=2)
  'CSE - A' => int 1
  'CSE - G' => int 0

在其他数组中缺席重复任务。

答案 1 :(得分:0)

试试这样。这里的$students样本数组与发布的数组完全相同。

    <?php
$present = 0;
$absent = 0;
$students = array(array('CSE-A'=>'PRESENT'),array('CSE-G'=>'ABSENT'),array('CSE-G'=>'ABSENT'));//Sampled Array
foreach($students as $key=>$array)
{
    foreach($array as $k=>$v)
    {
        if($array[$k] === 'PRESENT')
        {
            $present++;
        }
         else if ($array[$k] === 'ABSENT')
        {
            $absent++;
        }
    }
}
echo "Present Totals: ".$present.PHP_EOL;
echo "Absent Totals: ".$absent;

输出:

Present Totals: 1
Absent Totals: 2

答案 2 :(得分:0)

解决方案会是这样的:

创建一个多维$sectionRecords数组,其中包含所有部分以及相应数量的PRESENTABSENT s,如下所示:

$sectionRecords = array(
    'CSE-A' => array(
        'PRESENT' => 0,
        'ABSENT' => 0
    ),
    'CSE-B' => array(
        'PRESENT' => 0,
        'ABSENT' => 0
    ),

    ...

    'IT' => array(
        'PRESENT' => 0,
        'ABSENT' => 0
    ),
);

现在循环遍历原始数组,在每个部分上填充PRESENTABSENT s的数字,如下所示:

(假设$yourArray是您的原始数组)

foreach($yourArray as $arr){
    foreach($arr as $section => $status){
        if(array_key_exists($section, $sectionRecords)){
            ++$sectionRecords[$section][$status];
        }
    }
}

稍后,您会显示以下PRESENTABSENT的部分和相应数量:

foreach($sectionRecords as $section => $statArray){
    foreach($statArray as $status => $count){
        echo $section . ' ' . $status . ' ' . $count . '<br />';
    }
}

旁注:如果您想查看完整的$sectionRecords数组结构,请执行var_dump($sectionRecords);