我有像这样的指纹记录
"OUR COMPANY";"100";"100";07/25/2017 5:02:57 PM;"Check Out";"1";"";0;"Fingerpint";""
"OUR COMPANY";"102";"102";07/25/2017 7:45:53 AM;"Check In";"1";"";0;"Fingerpint";""
"OUR COMPANY";"102";"102";07/25/2017 7:45:54 AM;"Check In";"1";"";0;"Fingerpint";""
"OUR COMPANY";"102";"102";07/25/2017 6:01:34 PM;"Check Out";"1";"";0;"Fingerpint";""
"OUR COMPANY";"102";"102";07/25/2017 6:01:35 PM;"Check Out";"1";"";0;"Fingerpint";""
"OUR COMPANY";"104";"104";07/25/2017 8:00:06 AM;"Check Out";"1";"";0;"Fingerpint";""
"OUR COMPANY";"104";"104";07/25/2017 5:35:19 PM;"Check In";"1";"";0;"Fingerpint";""
"OUR COMPANY";"105";"105";07/25/2017 8:47:37 AM;"Check In";"1";"";0;"Fingerpint";""
"OUR COMPANY";"108";"108";07/25/2017 8:45:42 AM;"Check In";"1";"";0;"Fingerpint";""
"OUR COMPANY";"108";"108";07/25/2017 6:04:00 PM;"Check Out";"1";"";0;"Fingerpint";""
"OUR COMPANY";"11";"11";07/25/2017 8:57:34 AM;"Check In";"1";"";0;"Fingerpint";""
"OUR COMPANY";"11";"11";07/25/2017 5:12:06 PM;"Check Out";"1";"";0;"Fingerpint";""
"OUR COMPANY";"112";"112";07/25/2017 8:43:56 AM;"Check In";"1";"";0;"Password";""
"OUR COMPANY";"112";"112";07/25/2017 5:39:26 PM;"Check Out";"1";"";0;"Password";""
"OUR COMPANY";"112";"112";07/25/2017 5:41:17 PM;"Check Out";"1";"";0;"Password";""
格式化:
Array
(
[0] => Array
(
[0] => "OUR COMPANY"
[1] => "100"
[2] => "100"
[3] => 07/25/2017 5:02:57 PM
[4] => "Check Out"
[5] => "1"
[6] => ""
[7] => 0
[8] => "Fingerpint"
[9] => ""
)
[1] => Array
(
[0] => "OUR COMPANY"
[1] => "102"
[2] => "102"
[3] => 07/25/2017 7:45:53 AM
[4] => "Check In"
[5] => "1"
[6] => ""
[7] => 0
[8] => "Fingerpint"
[9] => ""
)
[2] => Array
(
[0] => "OUR COMPANY"
[1] => "102"
[2] => "102"
[3] => 07/25/2017 7:45:54 AM
[4] => "Check In"
[5] => "1"
[6] => ""
[7] => 0
[8] => "Fingerpint"
[9] => ""
)
...
)
我只希望获得每次登记和结账的第一条记录。该记录包含每个ID的多个条目,例如id 102有2个Check Ins和2个Check Out。我只想获得Check In的第一条记录和Check Out的第一条记录。这是我的方法。
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
if ($c == 1 || $c == 2 || $c == 3 || $c == 4) {
if($d != 0){
$dtary[$d][$c] = $data[$c] ;
}
}
if ($c == 9 ) {
$d++ ;
}
}
}
如何设置条件?
答案 0 :(得分:1)
这是您的预期结果吗?
"OUR COMPANY";"100";"100";07/25/2017 5:02:57 PM;"Check Out";"1";"";0;"Fingerpint";""
"OUR COMPANY";"102";"102";07/25/2017 7:45:53 AM;"Check In";"1";"";0;"Fingerpint";""
"OUR COMPANY";"102";"102";07/25/2017 6:01:34 PM;"Check Out";"1";"";0;"Fingerpint";""
"OUR COMPANY";"104";"104";07/25/2017 8:00:06 AM;"Check Out";"1";"";0;"Fingerpint";""
"OUR COMPANY";"104";"104";07/25/2017 5:35:19 PM;"Check In";"1";"";0;"Fingerpint";""
"OUR COMPANY";"105";"105";07/25/2017 8:47:37 AM;"Check In";"1";"";0;"Fingerpint";""
"OUR COMPANY";"108";"108";07/25/2017 8:45:42 AM;"Check In";"1";"";0;"Fingerpint";""
"OUR COMPANY";"108";"108";07/25/2017 6:04:00 PM;"Check Out";"1";"";0;"Fingerpint";""
"OUR COMPANY";"11";"11";07/25/2017 8:57:34 AM;"Check In";"1";"";0;"Fingerpint";""
"OUR COMPANY";"11";"11";07/25/2017 5:12:06 PM;"Check Out";"1";"";0;"Fingerpint";""
"OUR COMPANY";"112";"112";07/25/2017 8:43:56 AM;"Check In";"1";"";0;"Password";""
"OUR COMPANY";"112";"112";07/25/2017 5:39:26 PM;"Check Out";"1";"";0;"Password";""
您应该尝试array_intersect。
如果约束仅为ID and 'Check In/Out'
,而不考虑每天Datetime
或唯一 Check In/Out
,请查看以下内容:
<?php
$myArray = array();
$myArray[] = array("OUR COMPANY","100","100","07/25/2017 5:02:57 PM","Check Out","1","",0,"Fingerpint","");
$myArray[] = array("OUR COMPANY","102","102","07/25/2017 7:45:53 AM","Check In","1","",0,"Fingerpint","");
$myArray[] = array("OUR COMPANY","102","102","07/25/2017 7:45:54 AM","Check In","1","",0,"Fingerpint","");
$myArray[] = array("OUR COMPANY","102","102","07/25/2017 6:01:34 PM","Check Out","1","",0,"Fingerpint","");
$myArray[] = array("OUR COMPANY","102","102","07/25/2017 6:01:35 PM","Check Out","1","",0,"Fingerpint","");
$myArray[] = array("OUR COMPANY","104","104","07/25/2017 8:00:06 AM","Check Out","1","",0,"Fingerpint","");
$myArray[] = array("OUR COMPANY","104","104","07/25/2017 5:35:19 PM","Check In","1","",0,"Fingerpint","");
$myArray[] = array("OUR COMPANY","105","105","07/25/2017 8:47:37 AM","Check In","1","",0,"Fingerpint","");
$myArray[] = array("OUR COMPANY","108","108","07/25/2017 8:45:42 AM","Check In","1","",0,"Fingerpint","");
$myArray[] = array("OUR COMPANY","108","108","07/25/2017 6:04:00 PM","Check Out","1","",0,"Fingerpint","");
$myArray[] = array("OUR COMPANY","11","11","07/25/2017 8:57:34 AM","Check In","1","",0,"Fingerpint","");
$myArray[] = array("OUR COMPANY","11","11","07/25/2017 5:12:06 PM","Check Out","1","",0,"Fingerpint","");
$myArray[] = array("OUR COMPANY","112","112","07/25/2017 8:43:56 AM","Check In","1","",0,"Password","");
$myArray[] = array("OUR COMPANY","112","112","07/25/2017 5:39:26 PM","Check Out","1","",0,"Password","");
$myArray[] = array("OUR COMPANY","112","112","07/25/2017 5:41:17 PM","Check Out","1","",0,"Password","");
/**
* Compare two arrays and return true if intersected counts are the same.
*/
function in_array_intersect ($target, $haystack) {
foreach ($haystack as $item) {
if ( (count(array_intersect(array($item[1], $item[4]), $target)) == count($target))) {
return true;
}
}
return false;
}
// Define a new array to hold the matched records
$resultArray = array();
foreach ($myArray as $subArray) {
// Create a new array with only two columns, ID and CheckIn/Out
$target = array($subArray[1], $subArray[4]);
if (!in_array_intersect($target, $resultArray)){
// Add $subArray to $resultArray if no array is found in $resultArray by matching the columns constraint
array_push($resultArray, $subArray);
}
}
echo '<pre>';
print_r($resultArray);
echo '</pre>';
?>
<强>结果:强>
Array
(
[0] => Array
(
[0] => OUR COMPANY
[1] => 100
[2] => 100
[3] => 07/25/2017 5:02:57 PM
[4] => Check Out
[5] => 1
[6] =>
[7] => 0
[8] => Fingerpint
[9] =>
)
[1] => Array
(
[0] => OUR COMPANY
[1] => 102
[2] => 102
[3] => 07/25/2017 7:45:53 AM
[4] => Check In
[5] => 1
[6] =>
[7] => 0
[8] => Fingerpint
[9] =>
)
[2] => Array
(
[0] => OUR COMPANY
[1] => 102
[2] => 102
[3] => 07/25/2017 6:01:34 PM
[4] => Check Out
[5] => 1
[6] =>
[7] => 0
[8] => Fingerpint
[9] =>
)
[3] => Array
(
[0] => OUR COMPANY
[1] => 104
[2] => 104
[3] => 07/25/2017 8:00:06 AM
[4] => Check Out
[5] => 1
[6] =>
[7] => 0
[8] => Fingerpint
[9] =>
)
[4] => Array
(
[0] => OUR COMPANY
[1] => 104
[2] => 104
[3] => 07/25/2017 5:35:19 PM
[4] => Check In
[5] => 1
[6] =>
[7] => 0
[8] => Fingerpint
[9] =>
)
[5] => Array
(
[0] => OUR COMPANY
[1] => 105
[2] => 105
[3] => 07/25/2017 8:47:37 AM
[4] => Check In
[5] => 1
[6] =>
[7] => 0
[8] => Fingerpint
[9] =>
)
[6] => Array
(
[0] => OUR COMPANY
[1] => 108
[2] => 108
[3] => 07/25/2017 8:45:42 AM
[4] => Check In
[5] => 1
[6] =>
[7] => 0
[8] => Fingerpint
[9] =>
)
[7] => Array
(
[0] => OUR COMPANY
[1] => 108
[2] => 108
[3] => 07/25/2017 6:04:00 PM
[4] => Check Out
[5] => 1
[6] =>
[7] => 0
[8] => Fingerpint
[9] =>
)
[8] => Array
(
[0] => OUR COMPANY
[1] => 11
[2] => 11
[3] => 07/25/2017 8:57:34 AM
[4] => Check In
[5] => 1
[6] =>
[7] => 0
[8] => Fingerpint
[9] =>
)
[9] => Array
(
[0] => OUR COMPANY
[1] => 11
[2] => 11
[3] => 07/25/2017 5:12:06 PM
[4] => Check Out
[5] => 1
[6] =>
[7] => 0
[8] => Fingerpint
[9] =>
)
[10] => Array
(
[0] => OUR COMPANY
[1] => 112
[2] => 112
[3] => 07/25/2017 8:43:56 AM
[4] => Check In
[5] => 1
[6] =>
[7] => 0
[8] => Password
[9] =>
)
[11] => Array
(
[0] => OUR COMPANY
[1] => 112
[2] => 112
[3] => 07/25/2017 5:39:26 PM
[4] => Check Out
[5] => 1
[6] =>
[7] => 0
[8] => Password
[9] =>
)
)
答案 1 :(得分:0)
您需要loop
数组,build
新array
,如下所示
1st:在循环中为每个check In
存储unique id
的第一个条目然后忽略该ID的所有其他check In
条目。
第二名:在循环中为每个last entry
存储check Out
的{{1}}所以您只需unique id
每个条目。 overwrite
PHP:
if($val['4']=="Check Out"){.. } .
输出:
<?php
$data = Array(
"0" => Array
(
"0" => "OUR COMPANY",
"1" => "100",
"2" => "100",
"3" => "07/25/2017 5:02:57 PM",
"4" => "Check Out",
"5" => "1",
"6" => "",
"7" => 0,
"8" => "Fingerpint",
"9" => ""
),
"1" => Array
(
"0" => "OUR COMPANY",
"1" => "102",
"2" => "102",
"3" => "07/25/2017 7:45:53 AM",
"4" => "Check In",
"5" => "1",
"6" => "",
"7" => 0,
"8" => "Fingerpint",
"9" => ""
),
"2" => Array
(
"0" => "OUR COMPANY",
"1" => "102",
"2" => "102",
"3" => "07/25/2017 7:45:54 AM",
"4" => "Check In",
"5" => "1",
"6" => "",
"7" => 0,
"8" => "Fingerpint",
"9" => ""
),
"3" => Array
(
"0" => "OUR COMPANY",
"1" => "100",
"2" => "100",
"3" => "07/25/2017 6:02:57 PM",
"4" => "Check In",
"5" => "1",
"6" => "",
"7" => 0,
"8" => "Fingerpint",
"9" => ""
),
"4" => Array
(
"0" => "OUR COMPANY",
"1" => "102",
"2" => "102",
"3" => "07/25/2017 8:45:54 AM",
"4" => "Check Out",
"5" => "1",
"6" => "",
"7" => 0,
"8" => "Fingerpint",
"9" => ""
),
);
$final_array=array();
foreach($data as $key=>$val)
{
if(!isset($final_array[$val['1']]['check_in']) && $val['4']=="Check In"){
$final_array[$val['1']]['check_in']=$val[3];
}
if($val['4']=="Check Out"){
$final_array[$val['1']]['check_out']=$val[3];
}
}
echo "<pre>";
print_r($final_array);
?>