计算数组中的json对象

时间:2014-02-06 12:28:11

标签: php mysql json

努力获取数组中所有对象的计数以在json中吐出数字。有人可以帮忙吗?

try {
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   

    $stmt = $conn->prepare('SELECT * FROM date_blocks WHERE user_id = :user_id');
    $stmt->execute(array('user_id' => $user_id));

    while($row = $stmt->fetch()) {
        $startTime = strtotime($row['start_date']);
        $endTime = strtotime($row['end_date']);


        for ($i = $startTime; $i <= $endTime; $i = $i + 86400) {
            $getDate = date('Y-m-d H:i:s', $i); 
            $return[]=array('date'=>$getDate,
                    'id'=>$row['id']);
        }


    }
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
header('Content-type: application/json');
echo json_encode($return);

1 个答案:

答案 0 :(得分:1)

获取JSON格式的元素数量,您可以在脚本的末尾执行此操作:

header('Content-type: application/json');
echo json_encode(count($return));