如何从多维客户工单数组中统计工单总数与总时长?
Hey there, based on your requirement to extract two key stats from your ticket array, here's a straightforward approach:
What we need to do
We have two core tasks:
- Count the total number of tickets using
ticket_id - Calculate the sum of all
total_timevalues across all tickets
Your sample ticket array (cleaned up for clarity)
Array (
[0] => Array (
[ticket_id] => 7098246
[ticket_date_time] => 2/20/2018 17:22:35
[0] => Array (
[call_type] => No Call
[caller_type] => Other
[start_time] => 4/3/2018 10:29:06
[end_time] => 4/3/2018 10:30:06
[total_time] => 00:01:00
)
[1] => ... (additional call records for this ticket)
)
)
Implementation (using PHP as an example)
Assuming this is a PHP array, here's how to get the stats:
1. Total number of tickets
Since each element in the top-level array represents a ticket (with a ticket_id), we can just use the count() function:
$totalTickets = count($ticketArray); echo "Total tickets: " . $totalTickets;
2. Sum of all total_time values
First, we need to convert each HH:MM:SS string to seconds, sum them up, then convert back to a readable time format:
$totalSeconds = 0; // Loop through each ticket foreach ($ticketArray as $ticket) { // Loop through each call record in the ticket foreach ($ticket as $callRecord) { // Make sure the total_time key exists to avoid errors if (isset($callRecord['total_time'])) { // Split the time string into hours, minutes, seconds list($hours, $minutes, $seconds) = explode(':', $callRecord['total_time']); // Add to total seconds $totalSeconds += ($hours * 3600) + ($minutes * 60) + $seconds; } } } // Convert total seconds back to HH:MM:SS $hours = floor($totalSeconds / 3600); $remainingSeconds = $totalSeconds % 3600; $minutes = floor($remainingSeconds / 60); $seconds = $remainingSeconds % 60; $totalTime = sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds); echo "Total time across all tickets: " . $totalTime;
Quick notes
- Double-check that your array structure is consistent (every ticket has the same nested keys) to avoid missing data
- If your array uses a different language (like Python, JavaScript), the logic stays similar—just adjust the syntax for loops and string handling
内容的提问来源于stack exchange,提问作者Jerry R.

