2

Struggling to get the count of all objects in my array to spit out number in json. Can anyone help?

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);
0

1 Answer 1

1

Getting the number of elements in JSON format you could do this at the end of your script:

header('Content-type: application/json');
echo json_encode(count($return));
Sign up to request clarification or add additional context in comments.

1 Comment

That simple, duh. Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.