[英]PHP get the sum of hours of the two arrays
我有一個像下面這樣的數組,它里面有2個具有值的數組,想知道如何獲取兩個小時的總和,例如01:00 + 04:00 = 05:00
Array
(
[0] => stdClass Object
(
[total_time] => 01:00
}
[1] => stdClass Object
(
[total_time] => 04:00
}
}
謝謝。
有很多不同的方法可以實現這一目標。 我更喜歡使用array_map從對象中獲取值並轉換值。 對於array_sum來說,將它們加在一起應該很容易。
$hours_array = array_map("get_hours", $array);
$results = array_sum($hours_array);
function get_hours($object) {
// Check values first
// For just hours.
list($hours,) = explode(":", $object->{'total_time'});
return $hours;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.