I currently have 2 arrays;
$last_12_months
- contains an array of the last 12 months from the current month
$app12_array_temp
- contains a query result with a count of appointments made in that month, and a month.
I need to be able to loop through each month, and assign an appointment count to that month.
As i am nesting the loop, i am getting this as a result:
[0,0,0,0,0,0,0,"1",0,0,0,0,0,0,"4",0,0,0,0,0,0,"2",0,0,0,0,0,0,"15",0,0,0,0,0,0,"9",0,0,0,0,0,0,"8",0,0,0,0,0,0,"2",0,0,0,0,0,0,"1",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
What I need is:
[0,0,0,0,1,4,2,15,9,8,2,1]
my current script is:
//Loop through months, and check if appointments happened in that month
foreach($last_12_months as $m => $month){
foreach($app_12_array_temp AS $app){
if(date("m", strtotime($app['time'])) == date("m", strtotime($month))){
$app_12_array[] = $app['count'];
break;
}else{
$app_12_array[] = 0;
}
}
}
So if there is an appointment in that month, it should add the appointment count to an array, else add 0. Currently, it is adding 0 every time the loop isnt that corresponding month
I have tried using Breaks when condition met but will return 0 up until it finds that data.
I have also tried In_Array()
, but just returns [0,0,0,0,1,0,0,0,0,0,0,0]
This is what the script should be thinking, but I can't think of how to get it to do this:
Jan -> Any data for this month? -> No, so add 0 for this month to array
Feb -> Any data for this month? -> No, so add 0 for this month to array
Mar -> Any data for this month? -> Yes, so add $app['count'] to array
Apr -> Any data for this month? -> Yes, so add $app['count'] to array
May -> Any data for this month? -> Yes, so add $app['count'] to array
EDIT
$app_12_array_temp = [
['time' => '2019-07-27 13:00:00', 'count' => '1'],
['time' => '2019-08-26 13:00:00', 'count' => '2'],
['time' => '2019-09-06 13:00:00', 'count' => '8'],
['time' => '2019-10-22 12:00:00', 'count' => '9'],
['time' => '2019-11-21 12:00:00', 'count' => '15'],
['time' => '2019-12-27 11:00:00', 'count' => '2'],
['time' => '2020-01-22 15:00:00', 'count' => '4'],
['time' => '2020-02-12 09:00:00', 'count' => '1'],
];
$last_12_months = ['Feb', 'Jan', 'Dec', 'Nov', 'Oct', 'Sep', 'Aug', 'Jul', 'Jun', 'May', 'Apr', 'Mar'];
try this, using the month as the key for the new array
$app_12_array_temp = [
['time' => '2019-07-27 13:00:00', 'count' => '1'],
['time' => '2019-08-26 13:00:00', 'count' => '2'],
['time' => '2019-09-06 13:00:00', 'count' => '8'],
['time' => '2019-10-22 12:00:00', 'count' => '9'],
['time' => '2019-11-21 12:00:00', 'count' => '15'],
['time' => '2019-12-27 11:00:00', 'count' => '2'],
['time' => '2020-01-22 15:00:00', 'count' => '4'],
['time' => '2020-02-12 09:00:00', 'count' => '1'],
];
$last_12_months = ['Feb', 'Jan', 'Dec', 'Nov', 'Oct', 'Sep', 'Aug', 'Jul', 'Jun', 'May', 'Apr', 'Mar'];
$app_12_array = array_fill_keys( $last_12_months, 0 );
foreach ( $last_12_months as $m => $month )
{
foreach ( $app_12_array_temp AS $app )
{
if ( date( "m", strtotime( $app['time'] ) ) == date( "m", strtotime( $month ) ) )
{
$app_12_array[ $month ] = ($app_12_array[ $month ] ?? 0 ) + $app['count'];
break;
}
}
}
var_dump( $app_12_array );