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

I am trying to count the number of absenties and number of presenties per section in below example.but it is not easy to me how count number of absenties and presenties per section can u help me any one....thanks in advance

<?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) 
)

$table is your data. You have to loop over each element. Then for each new key, you create data in a $result array initialized with 0 value. If the value is present, you add 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);

Here an example result with the first value of your array:

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

Repeat task with absent in an other array.

Try like this.Here $students is a sample array exactly like as your posted array.

    <?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;

Output:

Present Totals: 1
Absent Totals: 2

The solution would be like this:

Create a multidimensional $sectionRecords array comprising of all the sections and the corresponding number of PRESENTs and ABSENTs, like this:

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

    ...

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

Now loop through the original array to populate above array with the number of PRESENTs and ABSENTs for each section, like this:

(Suppose $yourArray is your original array)

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

Later, you display the sections and the corresponding number of PRESENTs and ABSENTs like this:

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

Sidenote: If you want to see the complete $sectionRecords array structure, do var_dump($sectionRecords);