计算数组中的特定变量

时间:2019-02-20 21:39:05

标签: php arrays

我有一个带有startDate变量的数据数组-我该如何返回带有date变量的对象总数,然后只打印数字?

$date = Todays date

foreach ($results->data as $row) {

    $checkDate = $row->startDate;

    if(date('Y-m-d', strtotime($row->startDate)) == $date){

      Return total of date found
    }
}

1 个答案:

答案 0 :(得分:1)

只要找到一个计数器,就递增它:

$date = '2019-02-20';
$total = 0;

foreach ($results->data as $row) {   
    if(date('Y-m-d', strtotime($row->startDate)) == $date){
        $total++;  // same as $total = $total + 1;
    }
}
echo "$date found $total times";