我们如何使用php从数组中获取唯一数据的总和
I need to take sum of beds of unique date from array. I had tried with foreach but I am getting repeated data.
How can I get data like,
on 01.08.18 I have 3 bookings and sum of beds is 10
on 02.08.18 I have 3 bookings and sum of beds is 18
on 03.08.18 I have 2 bookings and sum of beds is 7
<?php
$dates = ['01.01.18', '02.01.18', '03.01.18']; //Dates are getting from daterange datepicker and preparing dates using php and stored in array.
foreach($dates as $date) {
// fetching data from table where date is matching with "$date".
$bookings = [
['date' => '01.01.18', 'beds' => '2'],
['date' => '01.01.18', 'beds' => '3'],
['date' => '01.01.18', 'beds' => '5'],
['date' => '02.01.18', 'beds' => '7'],
['date' => '02.01.18', 'beds' => '6'],
['date' => '02.01.18', 'beds' => '5'],
['date' => '03.01.18', 'beds' => '2'],
['date' => '03.01.18', 'beds' => '5'],
];
}
foreach($bookings as $booking) {
print_r($booking['date']);
}
我需要从数组中获取唯一日期的床的总和。 我曾尝试使用foreach,但我得到了重复的数据。 p>
如何在01.08.18获取数据, p>
我有 3个预订和床的总和是10 code> p>
02.08.18我有3个预订,床的总和是18 code> p>
on 03.08.18我有2个预订,床的总和是7 code> p>
&lt;?php
$ dates = [ '01 .01.18','02 .01.18','03 .01.18']; //日期来自daterange datepicker并使用php准备日期并存储在数组中。
foreach($ date as $ date){
//从日期与“$ date”匹配的表格中获取数据。
$ bookings = [
['date'=&gt; '01 .01.18','床'=&gt; '2'],
['date'=&gt; '01 .01.18','床'=&gt; '3'],
['date'=&gt; '01 .01.18','床'=&gt; '5'],
['date'=&gt; '02 .01.18','床'=&gt; '7'],
['date'=&gt; '02 .01.18','床'=&gt; '6'],
['date'=&gt; '02 .01.18','床'=&gt; '5'],
['date'=&gt; '03 .01.18','床'=&gt; '2'],
['date'=&gt; '03 .01.18','床'=&gt; '5'],
];
}
nachach($ bookings as $ booking){
print_r($ booking ['date']);
}
code> pre>
div>
Checkout the live result here: https://3v4l.org/nHijY
$arr = array();
foreach($bookings as $booking) {
$date = $booking['date'];
if(array_key_exists($date, $arr))
$arr[$date][] = $booking['beds'];
else
$arr[$date] = array($booking['beds']);
}
foreach($arr as $key => $value)
{
$count = count($value);
$sum = array_sum($value);
echo "on {$key} I have {$count} bookings and sum of beds is {$sum}
";
}
You can use array_reduce to iterate over bookings
array and for each booking update the sum if dates match:
Using array_reduce
$result = array_reduce( $bookings, function ($carry, $booking) use ($dates) {
$date = $booking['date'];
// Go to the next iteration if date doesn't exist
if ( !in_array( $date, $dates ) ) {
return $carry;
}
// Update sum if we have the same date Otherwise create new array
if ( isset( $carry[ $date ] ) ) {
$carry[$date]['sum'] += $booking['beds'];
$carry[$date]['count']++;
} else {
$carry[ $date ] = [ 'sum' => $booking['beds'], 'count' => 1 ];
}
return $carry;
});
Using a simple foreach
$result = [];
foreach($bookings as $booking) {
$date = $booking['date'];
// go to the next iteration if date doesn't exists
if ( !in_array( $date, $dates ) ) {
continue;
}
if ( isset( $result[ $date ] ) ) {
$result[$date]['sum'] += $booking['beds'];
$result[$date]['count']++;
} else {
$result[ $date ] = [ 'sum' => $booking['beds'], 'count' => 1 ];
}
}