0

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.

4
  • Where is your 3rd foreach loop? You're not matching emp id from array employee in array attendance Commented Mar 10, 2022 at 2:22
  • I believe the third arguments of array_search should be bool, not string. Have you tried changing it into bool type? Commented Mar 10, 2022 at 2:24
  • I think I've found the problem here. Probably because this line: $key_attd = array_search($range, array_column(json_decode($absen), 'checkin_time')); I search the data only using checkin_time value, where there's high posibility of employee having same value of checkin_time. Maybe this could be fixed if I also add another key such us employee_id when searching the array? If so how to do that? Commented Mar 10, 2022 at 2:25
  • @RaoDYC thanks, I think it happened because I don't filter it by employee when searching the data from attendance. Did I have to add another foreach or I can use multiple key when using array_search? Commented Mar 10, 2022 at 2:28

2 Answers 2

2

One way of solving this is by mapping and filtering your arrays like this:

$employees = [
    collect([
        "name" => "Employee 0",
        "employee_id" => "06cdc645-b783-4855-aa7d-32fa497d8335"
    ]),
    collect([
        "name" => "Employee 1",
        "employee_id" => "8bea471-641b-431a-829c-324b89e030d9"
    ]),
    collect([
        "name" => "Employee 2",
        "employee_id" => "384fe7a3-4adb-4fa6-b630-97d61ccbd4e7"
    ])
];

$attendances = [
    collect([
        "id" => "384fe7a3-4adb-4fa6-b630-97d61ccbd4e7",
        "employee_id" => "384fe7a3-4adb-4fa6-b630-97d61ccbd4e7",
        "attendance_time" => "2021-03-08 10:54:42",
        "checkin_time" => "2021-03-08",
        "in_out" => "in"
    ])
];

$intersection = array_map(function($employee) use ($attendances) {
    return array_filter($attendances, function($attendance) use ($employee) {
        return $employee->get('employee_id') == $attendance->get('employee_id');
    });
}, $employees);

By doing that the result of $intersection would be:

array:3 [▼
  0 => []
  1 => []
  2 => array:1 [▼
    0 => Illuminate\Support\Collection {#334 ▼
      #items: array:5 [▼
        "id" => "384fe7a3-4adb-4fa6-b630-97d61ccbd4e7"
        "employee_id" => "384fe7a3-4adb-4fa6-b630-97d61ccbd4e7"
        "attendance_time" => "2021-03-08 10:54:42"
        "checkin_time" => "2021-03-08"
        "in_out" => "in"
      ]
      #escapeWhenCastingToString: false
    }
  ]
]
Sign up to request clarification or add additional context in comments.

1 Comment

ah I see! thanks
0

I also found another way using array_filter like this:

$attendance = array_filter($decode_attendance, function ($var) use ($range, $employee_id) {
     return ($var['checkin_time'] == $range) && ($var['employee_id'] == $employee_id);
});

Using this I can get correct data by comparing range & employee_id

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.