1

I have a php array as follows :

Array
(
    [0] => Array
        (
            [EmployeeAttendance] => Array
                (
                    [attendance_id] => 1
                    [arrival_datetime] => 2016-11-01 03:31:00
                    [status] => Present
                )
        )

    [1] => Array
        (
            [EmployeeAttendance] => Array
                (
                    [attendance_id] => 2
                    [arrival_datetime] => 2016-11-03 06:31:00
                    [status] => Present
                )

        )

    [2] => Array
        (
            [EmployeeAttendance] => Array
                (
                    [attendance_id] => 3
                    [arrival_datetime] => 2016-11-06 07:31:00 
                    [status] => Present  
                )
        )

    [3] => Array
        (
            [EmployeeAttendance] => Array
                (
                    [attendance_id] => 4
                    [arrival_datetime] => 2016-11-08 19:31:00
                    [status] => Present
                )
        )

    [4] => Array
        (
            [EmployeeAttendance] => Array
                (
                    [attendance_id] =>5
                    [arrival_datetime] => 2016-11-10 08:00:00
                    [status] => Present
                )
        )
) 

Here in this array we can see the attendance details from date '2016-11-01' to '2016-11-10' and some dates are missing here like '2016-11-02','2016-11-04','2016-11-05','2016-11-07' and '2016-11-09' . Now I want to check the missing dates from the date between '2016-11-01' to '2016-11-10' and insert those missing dates into the array with status = "absent". Give some suggestion please.

3
  • Is this array limited to only one month or any month can be there? Commented Nov 11, 2016 at 10:42
  • @d.coder just only for a particular month Commented Nov 11, 2016 at 10:44
  • Can we change the structure of above array? And what is the use of this array then fro you? Commented Nov 11, 2016 at 10:46

2 Answers 2

1

First collect all dates, sort, generate daterange between firts, and last then is missing insert

$data = [

    [
        'EmployeeAttendance' => [
            'attendance_id'=>1
            ,'arrival_datetime'=>'2016-11-01 03:31:00'
            ,'status'=>'Present'
        ]
    ]
,[
        'EmployeeAttendance' => [
            'attendance_id'=>1
            ,'arrival_datetime'=>'2016-11-05 03:31:00'
            ,'status'=>'Present'
        ]
    ]
    ,[
 'EmployeeAttendance' => [
        'attendance_id'=>1
        ,'arrival_datetime'=>'2016-11-11 03:31:00'
        ,'status'=>'Present'
    ]
]
];

$dates = [];
foreach ($data as $x => $d) {
    $dates[] = substr($d['EmployeeAttendance']['arrival_datetime'],0,10);
}
sort($dates);
$period = new DatePeriod(
    new DateTime($dates[0]),
    new DateInterval('P1D'),
    new DateTime(end($dates))
);
foreach ($period as $d) {
    $key = $d->format('Y-m-d');
    if (!in_array($key,$dates)) {
        $data[] = [
            'EmployeeAttendance' => [
                 'arrival_datetime'=>$key
                ,'status'=>'absent'
            ]
        ];
    }
}
print_r($data);
Sign up to request clarification or add additional context in comments.

5 Comments

Is it possible to insert the missing dates in a ascending order
simply sort after fill uasort($data,function ($a,$b) { return $a['EmployeeAttendance']['arrival_datetime'] > $b['EmployeeAttendance']['arrival_datetime']; });,
i have done it like this way : foreach ($dataas $key => $d) { $sort[$key] = strtotime($d['EmployeeAttendance']['arrival_datetime']); } array_multisort($sort, SORT_ASC, $data);
@AjayKrishnaDutta You could consider accepting the answer
@AjayKrishnaDutta I'm sorry troubling you, but none of your questions including this one has accepted answer, upvoting is not enough there is a tick bellow vote button
1

You can also try this:

    $a = array(array('EmployeeAttendance'=>array('attendance_id'=>1,'arrival_datetime'=>'2016-11-01 03:31:00','status'=>'Present')),array('EmployeeAttendance'=>array('attendance_id'=>2,'arrival_datetime'=>'2016-11-03 04:31:00','status'=>'Present')),array('EmployeeAttendance'=>array('attendance_id'=>3,'arrival_datetime'=>'2016-11-07 07:31:00','status'=>'Present')),array('EmployeeAttendance'=>array('attendance_id'=>4,'arrival_datetime'=>'2016-11-09 05:31:00','status'=>'Present')));
    foreach($a as $key=>$data){
        $presentDates[] =  date("j", strtotime($data['EmployeeAttendance']['arrival_datetime']));// will give array of present dates array(1,3,7,9);
    }
    $first = reset($presentDates);
    $last = end($presentDates);
    $total = range($first,$last); // will give all dates between start and end
    $absentArr = array_diff($total, $presentDates); // will give missing dates array(2,4,5,6,8)
    foreach($absentArr as $value){
    $a[]['EmployeeAttendance'] = array('arrival_datetime'=>'2016-11-'.$value,'status'=>'Absent');
    }
  print_r($a);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.