如何计算数组中的常用值

时间:2015-01-20 06:09:13

标签: php arrays

Array (
        [attendance_id] => 18 
        [attendance] => 1
        [student_id] => 1
        [date] => 2015-01-19
        [in_time] => 00:00:00 
        [out_time] => 00:00:00
        [fee_amount] => 15000 ) 
Array (
        [attendance_id] => 19 
        [attendance] => 1 
        [student_id] => 2
        [date] => 2015-01-19
        [in_time] => 00:00:00
        [out_time] => 00:00:00 
        [fee_amount] => 2000 ) 
Array (
        [attendance_id] => 20 
        [attendance] => 0
        [student_id] => 1
        [date] => 2014-01-15 
        [in_time] => 00:00:00 
        [out_time] => 00:00:00 
        [fee_amount] => 0 
      )

我想计算这些数组中的常用值。请指导如何执行此操作。

结果我想得到这个:

 date      [2014-01-15 ] => 2
 attendance [1]  => 2

这实际上是针对一个小型学院,其中一天的单个学生详细信息可以如上所述进入系统。所以我只想在报告日结束时说明有多少学生在特定的日子上课,有多少没有。

2 个答案:

答案 0 :(得分:0)

试试这个..

   <?php
$array = array(array('18','2015-01-19','15000'),array('2015-01-19','18','22'),array('20','11','22'));

$newcountarray = array();
foreach ($array as $key=>$value) {
foreach ($value as $newvalue) {
    if ($foundKey = array_key_exists($newvalue,$newcountarray)) {
        $newcountarray[$newvalue] += 1;
    }
   else{
      $newcountarray[$newvalue] = 1;
   }
}}

print_r($newcountarray);

?>

<强>输出

 Array ( [18] => 2 [2015-01-19] => 2 [15000] => 1 [22] => 2 [20] => 1 [11] => 1 )

答案 1 :(得分:0)

是Deena让我们说

$array = array(array('18',<==row id no 
       '2015-01-19',<=date
       '1',<=student id>   
       '2'<=present(1),absent(2))),

       array(array('19',<==row id no 
       '2015-01-19',<=date
       '2',<=student id>   
       '1'<=present(1),absent(2))),

就像2015-01-19那天我希望得到所有2个学生,其中1个学生在场,1个缺席