Last active
March 19, 2020 07:32
-
-
Save IronGhost63/86be8076432f0a36e252426d8568763e to your computer and use it in GitHub Desktop.
reformat clock-in clock-out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ประกาศตัวแปรรอทิ้งไว้ จริงๆ ใช้ $data อันเดียวก็ได้ แล้ว map ทับเอา | |
$raw = []; | |
$data = []; | |
// ข้อมูลตัวอย่าง | |
$timein = [ | |
[ | |
'id' => 100287, | |
'date' => '2020-02-26', | |
'time' => '07:26:00', | |
], | |
[ | |
'id' => 100287, | |
'date' => '2020-02-26', | |
'time' => '17:06:00', | |
], | |
[ | |
'id' => 100287, | |
'date' => '2020-02-27', | |
'time' => '07:17:00', | |
], | |
[ | |
'id' => 100287, | |
'date' => '2020-02-27', | |
'time' => '17:05:00', | |
], | |
[ | |
'id' => 100287, | |
'date' => '2020-02-28', | |
'time' => '07:19:00', | |
], | |
[ | |
'id' => 100287, | |
'date' => '2020-02-28', | |
'time' => '12:19:00', | |
], | |
[ | |
'id' => 100287, | |
'date' => '2020-02-28', | |
'time' => '17:03:00', | |
], | |
[ | |
'id' => 100287, | |
'date' => '2020-02-29', | |
'time' => '07:19:00', | |
], | |
]; | |
// เอาข้อมูลมาจัดเรียงใหม่ก่อน ให้ id+date กำหนดเป็น unique key ของ พนง แต่ละคนในแต่ละวัน | |
foreach ( $timein as $time ) { | |
$key = $time['id'] . '|' . $time['date']; | |
$raw[$key][] = $time['time']; | |
} | |
// แมพข้อมูลเพื่อจัดรูปแบบใหม่ | |
$data = array_map( function( string $key, array $time ) { | |
// แตกคีย์ที่รวมไว้กลับออกมาเป็น id และ date เหมือนเดิม | |
$fragment = explode( '|', $key ); | |
// สั่งเรียงเวลาใหม่ก่อน | |
sort( $time ); | |
// หาเวลาตอกบัตรเข้าออก จากเวลาน้อยสุดและมากสุด ถ้าไม่มีตอกบัตรออกให้เป็น null | |
$checkin = reset( $time ); | |
$checkout = count( $time ) > 1 ? end( $time ) : null; | |
// เรียงข้อมูลใหม่แล้วคืนค่ากลับออกไป | |
return [ | |
'id' => $fragment[0], | |
'date' => $fragment[1], | |
'timein' => $checkin, | |
'timeout' => $checkout, | |
]; | |
}, array_keys( $raw ), $raw); | |
// ผ่าง | |
var_dump( $data ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment