0๐
โ
As the question was about sorting multi-dimesional array based on the key, try this
// remove dates older than 30 days
function remove30DayBeforeDates($a) {
$pieces = explode(' ', $a['datetime']);
return strtotime($pieces[0]) > strtotime('-30 days');
}
// remove dates older than 30 days
$array = array(
array(
'datetime' => '3/29/2019 4:00:00 PM - 5:00:00 PM',
'value_raw' => 95
), array(
'datetime' => '4/01/2019 3:00:00 PM - 4:00:00 PM',
'value_raw' => 99
), array(
'datetime' => '3/31/2019 2:00:00 PM - 3:00:00 PM',
'value_raw' => 101
), array(
'datetime' => '2/10/2019 2:00:00 PM - 3:00:00 PM',
'value_raw' => 112
), array(
'datetime' => '3/30/2019 2:00:00 PM - 3:00:00 PM',
'value_raw' => 100
));
$array = array_filter($array, "remove30DayBeforeDates");
// sort array based on the date
function check($a, $b) {
$pieces1 = explode(' ', $a['datetime']);
$pieces2 = explode(' ', $b['datetime']);
return strtotime($pieces2[0]) <=> strtotime($pieces1[0]); // use spaceship operator
}
// sort array based on the date
usort($array, "check");
$value_raw = array_column($array, 'value_raw'); // get array of values from column "value_raw"
print_r(implode(',',$value_raw));
0๐
$today= date('Y-m-d');
$thirty_days_ago = date('Y-m-d', strtotime('-30 days'));
then, in db query builder,
$data = DB::table('your_table_name')
->where('datetime','>='$thirty_days_ago)
->where('datetime','<='$today)
->whereRaw('value_raw = (select max(`value_raw`) from your_table_name)')
->select('datetime','value_raw')
->groupBy('datetime')->get();
0๐
You can fire query if you are using mysql to group by date and get max value,
SELECT MAX(value_raw), DATE(mydate) DateOnly FROM a_table where mydate BETWEEN (NOW() - INTERVAL 30 DAY) AND NOW() GROUP BY DateOnly;
0๐
Hi guys this is what i have come up with:
data: [
<?php
for($i=0; $i<=7; $i++)
{
$date = date('n/j/Y', strtotime('-'.$i.' days',strtotime(date('n/d/Y'))));
$filterBy = $date;
$new_array = array_filter($my_array, function ($array) use ($filterBy) {
$datetime = date("n/j/Y",changeFormat($array['datetime']));
if ($datetime == $filterBy)
{
return $datetime;
}
});
if (count($new_array) != 0)
{
$max_value = max(array_column($new_array, 'value_raw'));
echo number_format($max_value, 0, '.', '').",";
}
}
?> ],
Source:stackexchange.com