Skip to content
Advertisement

Search data from array of object inside foreach loop

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

IlluminateSupportCollection {#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.

Advertisement

Answer

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 => IlluminateSupportCollection {#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
    }
  ]
]
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement