如果我有这个数组:
Array (
[0] => Array (
[booking_id] => 1
[booking_status] => confirmed
[client_name] => Bale
[client_firstname] => Gareth
[days] => Array (
[day_id] => 2016-11-23,2016-11-24
[room_id] => 2
)
)
)
如何才能将商品数量转换为day_id
?
答案 0 :(得分:0)
$sum = 0;
foreach ($data as $booking) {
$sum += sizeof(explode(",",$booking["days"]["day_id"]));
}
答案 1 :(得分:0)
您可以使用array_reduce
迭代数组项并获得最终结果。看看this:
function reduce_func($carry, $item){
if (isset($item['days']) && !empty($item['days']['day_id'])){
$carry += count(array_unique(array_filter(explode(',', $item['days']['day_id']))));
}
return $carry;
}
$result = array_reduce($arr, "reduce_func", 0);
<强>更新强>
请注意,您还可以向array_filter
提供回调,以过滤掉您要计算的所需条目,例如。正则表达式检查每个条目是否为有效日期。这是PHP manual for array_filter
。
答案 2 :(得分:0)
$sum =0;
for ($count = 0; $count < count($data); $count++) {
$booking = $data[$count];
$day_ids = explode(",", $booking['days']['day_id'];
$sum = $sum + count($day_ids);
}