I have a big set of time records for a project and I want to filter out all but those posted by a single employee.
array_walk($timeRecords, function($timeRecord, $index) use ($employee) {
if ($timeRecord->employeeId != $employee->id) {
unset($timeRecords[$index]);
}
});
You can see what I'm trying to do. How do you go about doing this with anon functions and closures? Obviously $timeRecords is not defined inside the anonymous function. Thanks.
Calimero pointed out that's the wrong tool for the job. The desired effect can be achieved using array_filter. Array_walk appears to be designed for modifying individual array items by reference.
This is how to achieve what I was wanting.
$timeRecords = array_filter($timeRecords, function($timeRecord) use ($employee) {
if ($timeRecord->EmployeeId == $employee->EmployeeId) {
return $timeRecord;
}
});