1👍
From the query you will get hour and sum if so.. you can try the bellow code
$results = array(array('hour' => 2, 'count' => 3), array('hour' => 3, 'count' => 3), array('hour' => 13, 'count' => 10));
$hours = array(1,2,3,4,5,6,7,8,9,10,11,12,13); //up to 24
$finalResult = [];
foreach($hours as $hour){
$hourSearch = array_search($hour, array_column($results, 'hour'));
if(is_numeric($hourSearch)) {
// array_push($finalResult, $results[$hourSearch]);
array_push($finalResult,$results[$hourSearch]['count']);
} else {
// $data['hour'] = $hour;
// $data['count'] = 0;
//array_push($finalResult, $data);
array_push($finalResult,0);
}
}
echo json_encode($finalResult);
http://sandbox.onlinephpfunctions.com/code/d3c04e3b58b01b9f70652a3398379d867453b0ea
- Chartjs-Django chart.js – Query to get count of all distinct values for particular column
- Chartjs-Chartjs v3 overlap stacked bar chart with groups
1👍
i have read the answer by @Shibon and its looks good
i have a Same Situation while creating the charts based on
https://github.com/ConsoleTVs/Charts
So I have created the function for it
Here is my array only with values but i need to set the other values to zero so i have created my own function
$arrayWithValues =
[
'02' => '20',
'09' => '45',
'15' => '68',
'21' => '28'
];
$defaultEmptyArray = [
'00' => '0',
'01' => '0',
'02' => '0',
'03' => '0',
'04' => '0',
'05' => '0',
'06' => '0',
'07' => '0',
'08' => '0',
'09' => '0',
'10' => '0',
'11' => '0',
'12' => '0',
'13' => '0',
'14' => '0',
'15' => '0',
'16' => '0',
'17' => '0',
'18' => '0',
'19' => '0',
'20' => '0',
'21' => '0',
'22' => '0',
'23' => '0',
];
Now we need to replace the array only that has the value and i have written my own function
function setUnsettedArray($actulHourWithValue = [] ,$defaultEmptyArray = [])
{
$arraNotExists = [];
foreach ($defaultEmptyArray as $defKey => $defValue)
{
if (array_key_exists($defKey, $actulHourWithValue))
{
$arrayEXists[] = [$defKey => $actulHourWithValue[$defKey]];
}
elseif (!array_key_exists($defKey, $actulHourWithValue))
{
$arraNotExists[] = [$defKey => $defValue];
}
}
$newArray = array_merge($arraNotExists,$arrayEXists);
foreach ($newArray as $newKey => $newValue)
{
$keys[] = $newKey;
foreach ($newValue as $key => $value)
{
$allKesy[] = $key;
$allValues[] = $value;
}
}
$finalArray = array_combine($allKesy,$allValues);
ksort($finalArray);
return $finalArray;
}
and try to pass the array as print_r(setUnsettedArray($arrayWithValues,$defaultEmptyArray));
and here is the fiddle for it
http://phpfiddle.org/main/code/sfq3-adyn
I have EDITED the setUnsettedArray
function
function setUnsettedArray(array $arrayWithValues ,array $defaultArray)
{
$finalArray = $arrayWithValues+$defaultArray;
ksort($finalArray);
return $finalArray;
}
And i have seen Your Query and its not to good
So try this
$lastTwentyFour = Report::where('site_id','=',$site->id)
->whereDate('created_at', '=', Carbon::now()->subHours(24))
->orderBy('created_at')
->get()
->groupBy(function($date)
{
return Carbon::parse($date->created_at)->format('H');
}
);
and You can count the each hour collection as
foreach ($lastTwentyFour as $newGroupByKey => $newGroupByValue)
{
$eachHour[] = $newGroupByKey;
$eachHourCount[] = $newGroupByValue->count();
}
$actulHourWithValue = array_combine($eachHour,$eachHourCount);
1👍
I dont seem to understand your question exactly but using carbon would help alot when it comes to working with dates, it would reduce your code by a lot of lines.
`$now = Carbon\Carbon::now;
$hoursBefore = $now->subDays(1);
$reports = Report::where(‘site_id’, $site->id)->whereBetween(‘created_at’, [$hoursBefore, $now])->get()->toArray();
`
I hope this helps.
Thanks, good luck and Happy coding…. 🙂