自己个儿写的循环分配txt中的内容

时间:2021-08-29 16:01:23
 <?php
header("Content-type:text/html;charset=utf8");
//当前日期
$now = time();
//作为程序是否执行的依据
$flag = TRUE;
//返回数组key
function getKeys($department) {
$arrayitm = explode('|', $department);
$str ='';
foreach($arrayitm as $k => $v){
//判断当前位置
if(strstr($v, '1')){
$str .= $k;
}else{
$str .= '#';
}
}
//正则取数字
$result=preg_match_all('/\d+/',$str,$arr);
//print_r($arr);
if(!empty($arr[0])){
return $arr[0][0];
}else{
//默认
return '-1';
}
}
//读取log日志
$file_log ='datelog.txt';
$logs = file_get_contents($file_log);
$datetime = date('Y-m-d', $now);
//$datetime = "2017-03-31"; //测试
//var_dump($logs);
//节假日列表
$exception = array(
'2017-01-01','2017-01-02','2017-01-27','2017-01-28','2017-01-29','2017-01-30','2017-01-31','2017-02-01','2017-02-02','2017-04-02','2017-04-03','2017-04-04','2017-04-29','2017-04-30','2017-05-01',
'2017-05-28','2017-05-29','2017-05-30','2017-10-01','2017-10-02','2017-10-03','2017-10-04','2017-10-05','2017-10-06','2017-10-07','2017-10-08'
);
//调休列表
$leaves = array(
// '2017-03-20','2017-03-21'
);
//echo date('Y-m-d', $starttime).'<br/>';
//$now = strtotime('2017-03-31'); //测试
$weekday = date('N', $now);//得到星期值:1-7 排除 周6 周天 6和7
//$weekday =6;
if(in_array($datetime, $exception)){
//法定节假日
$flag = false;
}elseif(in_array($datetime, $leaves)){
//调休
$flag = false;
}else{
if(empty($logs)){
//写入当前时间 判断周末 或者节假日
$result_date = file_put_contents($file_log, $datetime, LOCK_EX);
}else{
//读取内容 作判断
$logs_new = file_get_contents($file_log);
$arr = explode('|', $logs_new);
if(in_array($datetime, $arr)){
//作为程序是否执行的依据
$flag = false;
}else{
$flag = true;
//追加内容
$result_date = file_put_contents($file_log, '|'.$datetime, FILE_APPEND|LOCK_EX);
}
}
} $file_dep ='department.txt'; //内容,0|内容2,0|.....
$file_lea ='leader.txt'; //同上
$department = file_get_contents($file_dep);
//将数据分割
$arritem = explode('|', $department);
//print_r($arritem);
$length = count($arritem); //获取当前内容下标
$keys = getKeys($department);
//var_dump($flag);
if($flag){
if($keys == '-1'){
//默认时
$replace = str_replace('0', '1', $arritem[0]);
$arritem[0] = $replace;
}else{
$keys = intval($keys);
if($keys == $length -1){
//echo 'ok';
//到数组末端 重0开始
$replace = str_replace('0', '1', $arritem[0]);
$arritem[0] = $replace; $arritem[$keys] = str_replace('1', '0', $arritem[$keys]);
}else{
//echo 'ak1='.$arritem[$keys];
//清空前一个数据
$arritem[$keys] = str_replace('1', '0', $arritem[$keys]);
//更改 数组坐标 +1 的值
$zuo = $keys+1;
$replace = str_replace('0', '1', $arritem[$zuo]);
$arritem[$zuo] = $replace;
}
}
if(!empty($arritem)){
//将修改后的数据转成字符串存入txt
$write=implode('|',$arritem);
//echo $write;exit;
$result = file_put_contents($file_dep, $write, LOCK_EX);
if($result){
//echo '写入文件成功';
}
}
} /***************************************************************************************/
$leader = file_get_contents($file_lea);
//将数据分割
$arritem_lea = explode('|', $leader);
$length_lea = count($arritem_lea); //获取当前内容下标
$keys_lea = getKeys($leader);
//var_dump($flag);
if($flag){
if($keys_lea == '-1'){
//默认时
$replace = str_replace('0', '1', $arritem_lea[0]);
$arritem_lea[0] = $replace;
}else{
$keys_lea = intval($keys_lea);
if($keys_lea == $length_lea -1){
//到数组末端 重0开始
$replace = str_replace('0', '1', $arritem_lea[0]);
$arritem_lea[0] = $replace; $arritem_lea[$keys_lea] = str_replace('1', '0', $arritem_lea[$keys_lea]);
}else{
//清空前一个数据
$arritem_lea[$keys_lea] = str_replace('1', '0', $arritem_lea[$keys_lea]);
//更改 数组坐标 +1 的值
$zuo = $keys_lea+1;
$replace = str_replace('0', '1', $arritem_lea[$zuo]);
$arritem_lea[$zuo] = $replace;
}
}
if(!empty($arritem_lea)){
//将修改后的数据转成字符串存入txt
$write_lea=implode('|',$arritem_lea);
//echo $write;exit;
$result_lea = file_put_contents($file_lea, $write_lea, LOCK_EX);
if($result_lea){
//echo '写入文件成功';
}
}
}
//输出结果
if($department && $leader){
// echo 'keys='.$keys;
// echo "<br/>";
// echo 'keys_lea='.$keys_lea;
// echo "<br/>";
//echo $length;
if($keys == '-1'){
$k = 0;
}elseif($keys >= 0 ){
if(intval($keys) == $length - 1){
if($flag){
$k = 0;
}else{
$k = $keys;
}
}else{
if($flag){
$k = $keys+1;
}else{
$k = $keys;
}
}
} if($keys_lea == '-1'){
$k_lea = 0;
}elseif($keys_lea >= 0 ){
if(intval($keys_lea) == $length_lea - 1){
if($flag){
$k_lea = 0;
}else{
$k_lea = $keys_lea;
}
}else{
if($flag){
$k_lea = $keys_lea+1;
}else{
$k_lea = $keys_lea;
}
}
} // echo 'k='.$k;
// echo "<br/>";
// echo 'k_lea='.$k_lea;
// echo "<br/>";
// echo 'length='.$length;
//echo '<br/>';
//echo $weekday.'<br/>';
if (in_array($datetime, $exception)) {
echo '节假日轮休';
exit;
}elseif(in_array($datetime, $leaves)){
echo '调休';
exit;
}else{
$str1 = explode(',', $arritem[$k])[0];
$str2 = explode(',', $arritem_lea[$k_lea])[0];
echo $datetime.'<br/>';
echo '值班单位:'.$str1.'<br/>';
echo '值班领导:'.$str2;
}
}
exit;

需求: 按内容顺序循环分配  排除法定节假日  有点类似排班

原理:没有用数据库  用的txt 默认数据为0 执行程序后 值改为1 作为标识当前内容 获取下标 替换原有内容 在写入文件