我正在寻找可以将工作日的任何组合转换为二进制的反对的PHP类,反之亦然
0(星期日)至6(星期六)
示例:
周一,周二,周三,周四,周五=> 0111110(星期日和星期六休息日)
0101010 =>这个二进制代表星期一,星期三,星期五是ON其他是OFF天
该类应该能够将二进制文件转换为工作日和工作日为二进制文件
该课程可以有两种方法:
公共功能convertWeekdaysToBinary($ days)
公共函数convertBinaryToWeekdays($ binary)
答案 0 :(得分:1)
这是第一次:
$arr = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
$search1 = "Monday, Tuesday, Wednesday, Thursday, Friday";
$search_arr = explode(", ", $search1);
$out1 = array("0","0","0","0","0","0","0");
foreach($search_arr as $value){
$sr = array_search($value, $arr);
$out1[$sr] = "1";
}
echo implode("", $out1); //0111110
<强>反之亦然强>
$search2 = "0101010";
$search_arr = str_split($search2);
$out2 = array();
foreach($search_arr as $key => $value){
if($value == 1){
$out2[] = $arr[$key];
}
}
echo implode(", ", $out2);//Monday, Wednesday, Friday