The way I would approach this is to:
Filter the RAW input array to the earliest in
and the latest out
. We don't need the rest of the results.
Re-format that output in the format expected.
The dateTime
strings appear to be properly formatted time stamps (presumably from a mysqli database). Which is good because it means we can compare the timestamps with <
and >
.
Solution
// Define the filter array
$filterArray = [];
foreach($values as $log){
// Set a reference to this userId in the new array
$user = &$filterArray[$log["userId"]];
// If nothing has been added for this person's IN/OUT yet then add it on here
if(! ($user[$log["event"]] ?? null)){
$user[$log["event"]] = $log;
}
// If the event is IN then check to see if this IN is earlier than the one already stored
if($log["event"] == "IN" && $user["IN"]["dateTime"] > $log["dateTime"]){
$user["IN"] = $log["dateTime"];
}
// If the event is OUT then check to see if this OUT is later than the one already stored
if($log["event"] == "OUT" && $user["OUT"]["dateTime"] < $log["dateTime"]){
$user["OUT"] = $log;
}
}
// Sort by userId
ksort($filterArray);
// Define the output array
$outArray = [];
// Fill the output array
foreach($filterArray as $log){
$outArray[] = $log["IN"];
$outArray[] = $log["OUT"];
}
Output
array (
0 =>
array (
'logId' => 1001,
'userId' => 1111,
'dateTime' => '2021-01-04 08:11:50',
'event' => 'IN',
),
1 =>
array (
'logId' => 1003,
'userId' => 1111,
'dateTime' => '2021-01-04 17:10:50',
'event' => 'OUT',
),
2 =>
array (
'logId' => 1004,
'userId' => 2222,
'dateTime' => '2021-01-04 08:10:50',
'event' => 'IN',
),
3 =>
array (
'logId' => 1005,
'userId' => 2222,
'dateTime' => '2021-01-04 17:00:50',
'event' => 'OUT',
),
)
Code without comments
$filterArray = [];
foreach($values as $log){
$user = &$filterArray[$log["userId"]];
if(! ($user[$log["event"]] ?? null)){
$user[$log["event"]] = $log;
}
if($log["event"] == "IN" && $user["IN"]["dateTime"] > $log["dateTime"]){
$user["IN"] = $log["dateTime"];
}
if($log["event"] == "OUT" && $user["OUT"]["dateTime"] < $log["dateTime"]){
$user["OUT"] = $log;
}
}
ksort($filterArray);
$outArray = [];
foreach($filterArray as $log){
$outArray[] = $log["IN"];
$outArray[] = $log["OUT"];
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…