I have 3 array. The first one is array employee, second is array dateRange(Y-m-d), and the third is array attendance
The array is like this:
Array Employee
Illuminate\Support\Collection {#1521 ▼
#items: array:13 [▼
0 => {#1520 ▼
+"name": "Employee 1"
+"employee_id": "07cdc645-b783-4855-aa7d-32fa497d8335"
}
1 => {#1523 ▼
+"name": "Employee 2"
+"employee_id": "09bea471-641b-431a-829c-324b89e030d9"
}
2 => {#1547 ▼
+"name": "Employee 3"
+"employee_id": "385fe7a3-4adb-4fa6-b630-97d61ccbd4e7"
}
Array dateRange
array:3 [▼
0 => "2022-03-07"
1 => "2022-03-08"
2 => "2022-03-09"
]
Array Attendance (both in & out)
array:24 [▼
0 => {#1920 ▼
+"id": "385fe7a3-4adb-4fa6-b630-97d61ccbd4e7"
+"employee_id": "385fe7a3-4adb-4fa6-b630-97d61ccbd4e7"
+"attendance_time": "2022-03-08 10:54:42"
+"checkin_time": "2022-03-08"
+"in_out": "in"
}
etc...
]
What I want to do is to get attendance data from array attendance where employee_id is the same as employee_id in array employee.
What I've done is first I loop the array employee and then inside the loop I add another nested foreach to loop array dateRange. And then inside foreach array dateRange I've a code to search data.
<tr>
@foreach ($employee as $emp)
<tr>
<td>{{ $emp->employee_id }}</td>
@foreach ($dateRange as $key => $range)
@php
// Search data from array attendance where checkin_time is the same as $range
$key_attd = array_search($range, array_column(json_decode($attendance_in), 'checkin_time'));
// Search data from array attendance where checkout_timeis the same as $range
$key_attd_out = array_search($range, array_column(json_decode($attendance_out), 'checkout_time'));
// Decode json so I can use the key from result data above to search the attendance data
$decode_attendance = json_decode($attendance_in);
$decode_attendance_out = json_decode($attendance_out);
@endphp
// When key was found/not false it will get data from array attendance using the key
@if ($key_attd != false)
@if ($decode_attendance[$key_attd]->employee_id == $emp->employee_id)
<td>
{{ $decode_attendance[$key_attd]->attendance_time ?? '-' }}
</td>
@endif
@else
<td>-</td>
@endif
// Attendance out is the same, it will get attendance_out data using the key which is resulted from array search above
@if ($key_attd_out != false)
// When employee_id from attendance is the same as employee_id from employee
@if ($decode_attendance_out[$key_attd_out]->employee_id == $emp->employee_id)
<td>
{{ $decode_attendance_out[$key_attd_out]->attendance_time ?? '-' }}
</td>
@endif
@else
<td>-</td>
@endif
@endforeach
</tr>
@endforeach
But the code above still not working, I don't get the data correctly. I know there's something wrong in my code, but I don't know what it is.
array_searchshould bebool, notstring. Have you tried changing it intobooltype?$key_attd = array_search($range, array_column(json_decode($absen), 'checkin_time'));I search the data only usingcheckin_timevalue, where there's high posibility ofemployeehaving same value ofcheckin_time. Maybe this could be fixed if I also add another key such usemployee_idwhen searching the array? If so how to do that?I don't filter it by employeewhen searching the data fromattendance. Did I have to add another foreach or I can use multiple key when usingarray_search?