PHP push missing weekdays with 0 total into array

I need to buildup a graph with total sales for each day for last week, I did not get all 7 days data, rather getting 3 days data from database, I need to push rest of the 4days with total sales 0,

I have the following array of 3 days

Array
(
    [0] => Array
        (
            [SalesDate] => Jun09
            [total] => 4
        )

    [1] => Array
        (
            [SalesDate] => Jun11
            [total] => 2
        )

    [2] => Array
        (
            [SalesDate] => Jun14
            [total] => 1
        )

)

but I need all 7days data from Jun09 to Jun15 like this

Array
(
    [0] => Array
        (
            [SalesDate] => Jun09
            [total] => 4
        )

    [1] => Array
        (
            [SalesDate] => Jun10
            [total] => 0
        )

    [2] => Array
        (
            [SalesDate] => Jun11
            [total] => 2
        )
    [3] => Array
        (
            [SalesDate] => Jun12
            [total] => 0
        )
    [4] => Array
        (
            [SalesDate] => Jun13
            [total] => 0
        )
    [5] => Array
        (
            [SalesDate] => Jun14
            [total] => 1
        )
    [6] => Array
        (
            [SalesDate] => Jun15
            [total] => 0
        )
   
)

I have tried with following code, but not getting the required data.

<?php 
$sales =array(array('SalesDate' => 'Jun09',
                        'total' => 4
                        ),

                array
                    (
                       'SalesDate' => 'Jun11',
                        'total' => 2
                    ),
           
                array
                    (
                        'SalesDate' => 'Jun14',
                       'total' => 1
                    )
                );
$final_array = array();
foreach($sales as $sale)
            {
                for($i=7;$i>0;$i--)
                {
                    $current_weeks =[];
                    if($sale['SalesDate'] ==date('Md', strtotime("-".$i." days")))
                    {
                        $week_days['SalesDate'] = $sale['SalesDate'];
                        $week_days['total'] = $sale['total'];
                    }
                    else
                    {
                        $week_days['SalesDate'] = date('Md', strtotime("-".$i." days"));
                        $week_days['total'] = 0;
                       
                    }
                   $final_array[] =$week_days;
                }
               
                
            }

Answer

Solution:

You could first create a 'skeleton' array that matches your source array $arr, but with all the previous seven days $prevSevenDays. The date format matches the source array's, the totals are all set to 0.

// build empty skeleton
$prevSevenDays = [];
for ($i = 7; $i > 0; $i--) {
    $prevSevenDays[$i]['SalesDate'] = date('Md', strtotime("-$i days"));
    $prevSevenDays[$i]['Total'] = 0;
}

All you have to do is cycle through this array to replace the entries with available data in your source array $arr.

foreach ($arr as $sales) {
    foreach ($prevSevenDays as $key => $day) {
        if ($sales['SalesDate'] === $day['SalesDate']) $prevSevenDays[$key]['Total'] = $sales['Total'];
    }
}

demo

Source