Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Timeword—压缩一个天数数组,并将其表示为一组范围_Php_Days - Fatal编程技术网

Php Timeword—压缩一个天数数组,并将其表示为一组范围

Php Timeword—压缩一个天数数组,并将其表示为一组范围,php,days,Php,Days,我正在尝试创建一个函数,该函数将接受以下输入: 周一、周二、周三、周四、周五、周六 说出这个: mon-sat 目前,我正在使用这个 function dayrange($days){ $days = explode(",", str_replace(" ","",$days)); return reset($days) . "-" . end($days); } 好的,现在对于曲线球,如果我给它这个字符串,我希望函数也能工作: 周一、周二、周三、周五、周六 它需要跳过星期四,然后

我正在尝试创建一个函数,该函数将接受以下输入:
周一、周二、周三、周四、周五、周六

说出这个:
mon-sat

目前,我正在使用这个

function dayrange($days){
   $days = explode(",", str_replace(" ","",$days));
   return reset($days) . "-" . end($days);
}
好的,现在对于曲线球,如果我给它这个字符串,我希望函数也能工作:
周一、周二、周三、周五、周六

它需要跳过星期四,然后说: 周一周三,周五周六

我能得到关于最好的方法的建议吗? 我使用的是PHP,所以PHP中的答案很好,但psuedo代码也很好


“干杯”

并没有检验这一点,但应该会给你一个良好的开端。它还可以处理周包装

function dayrange($days){
    $wdays = array("mon","tue","wed","thu","fri","sat","sun");

    $indays = explode(",", str_replace(" ","",$days)); // expand the list to an array


    $retstr = array_shift($indays); // get the first date

    $curpos = array_search($retstr, $wdays);  // current position in the wdays array
    $intv = 0;    // interval between days to avoid mon-tue like output

    foreach($indays as $d) {
       if($d == $wdays[$curpos]) {
          $curpos = ($curpos++) % 7; // this will take care of wrapping.
          $intv++;
       } else {
           $retstr.= ($intv > 1 ? "-".$d:",".$d); // use appropriate join
           $intv = 0; // reset interval
       }
    }
    if($intv > 0) {   // if anything was left deal with the end.
        $retstr.= ($intv > 1 ? "-".$d:",".$d);
    } else {
        $retstr.= ",".$d;
    }
    return ($retstr);
}

还没有测试过这个,但是应该给你一个好的开始。它还可以处理周包装

function dayrange($days){
    $wdays = array("mon","tue","wed","thu","fri","sat","sun");

    $indays = explode(",", str_replace(" ","",$days)); // expand the list to an array


    $retstr = array_shift($indays); // get the first date

    $curpos = array_search($retstr, $wdays);  // current position in the wdays array
    $intv = 0;    // interval between days to avoid mon-tue like output

    foreach($indays as $d) {
       if($d == $wdays[$curpos]) {
          $curpos = ($curpos++) % 7; // this will take care of wrapping.
          $intv++;
       } else {
           $retstr.= ($intv > 1 ? "-".$d:",".$d); // use appropriate join
           $intv = 0; // reset interval
       }
    }
    if($intv > 0) {   // if anything was left deal with the end.
        $retstr.= ($intv > 1 ? "-".$d:",".$d);
    } else {
        $retstr.= ",".$d;
    }
    return ($retstr);
}

基本上,我会通过以下方式来实现这一点:

  • 将天数转换为相应的数值
  • 将数字数组转换为具有范围的字符串
  • 将字符串中的数字转换回一周中的天数
  • 为此,我编写了一些代码:

    /**
     * Convert an array of numbers to a string containing ranges and single values
     * @param array $numbers an array of numbers
     * @return string
     */
    function compressNumbers($numbers) {
        $result = array();
        sort($numbers);
        $previousValue = reset($numbers);
        $startValue = $previousValue;
        foreach ($numbers as $value) {
            if ($value > $previousValue + 1) {
                if ($startValue == $previousValue) {
                    $result[] = $startValue;
                } else {        
                    $result[] = $startValue . '-' . $previousValue;
                }
                $startValue = $value;
            }
            $previousValue = $value;
        }
        if ($startValue == $previousValue) {
            $result[] = $startValue;
        } else {        
            $result[] = $startValue . '-' . $previousValue;
        }
        return implode(',', $result);
    }
    
    /*
     * Creates an array with values the three letter representation for days of the 
     * week and keys the corresponding numeric representation.
     *
     * @return array
     */
    function createLookupNumberToDay() {
        $date = strtotime('now');
        $lookup = array();
        for ($i = 1; $i <= 7; $i++) {
            $lookup[date('w', $date)] = date('D', $date);
            $date = strtotime('+1 day', $date);
        }
        return $lookup;
    }
    
    /*
     * Converts a string listing days separated by commas into 
     * an array with values the numeric value for the corresponding
     * day of the week.
     *
     * @param string $days
     * @return array
     */
    function convertDaysToNumbers($days) {
        $result = array();
        $daysArray = explode(",", str_replace(" ","",$days));
        foreach ($daysArray as $day) {
            $result[] = date('w', strtotime($day));
        }
        return $result;
    }
    
    /*
     * Converts the numbers in a string to the corresponding 3-letter day of the
     * week abbreviation.
     *
     * @param string $string
     * @return string
     */
    function convertNumbersToDays($string) {
        $lookup = createLookupNumberToDay();
        return str_replace(array_keys($lookup), $lookup, $string);
    }
    
    function convert($string) {
        return (convertNumbersToDays(compressNumbers(convertDaysToNumbers($string))));
    }
    
    echo convert('mon,tue,wed,thu,fri,sat');
    echo '<br />';
    echo convert('mon,tue,wed,sat');
    echo '<br />';
    
    /**
    *将数字数组转换为包含范围和单个值的字符串
    *@param array$numbers一组数字
    *@返回字符串
    */
    函数压缩数($numbers){
    $result=array();
    排序(数字);
    $previousValue=重置($number);
    $startValue=$previousValue;
    foreach(数字为$value){
    如果($value>$previousValue+1){
    如果($startValue==$previousValue){
    $result[]=$startValue;
    }否则{
    $result[]=$startValue.'-'.$previousValue;
    }
    $startValue=$value;
    }
    $previousValue=$value;
    }
    如果($startValue==$previousValue){
    $result[]=$startValue;
    }否则{
    $result[]=$startValue.'-'.$previousValue;
    }
    返回内爆(“,”,$result);
    }
    /*
    *创建一个数组,该数组的值以三个字母表示,表示一天中的天数
    *week并为相应的数字表示形式设置关键帧。
    *
    *@return数组
    */
    函数createLookupNumberToDay(){
    $date=strottime('now');
    $lookup=array();
    
    对于($i=1;$i基本上,我将通过以下方式来实现这一点:

  • 将天数转换为相应的数值
  • 将数字数组转换为具有范围的字符串
  • 将字符串中的数字转换回一周中的天数
  • 为此,我编写了一些代码:

    /**
     * Convert an array of numbers to a string containing ranges and single values
     * @param array $numbers an array of numbers
     * @return string
     */
    function compressNumbers($numbers) {
        $result = array();
        sort($numbers);
        $previousValue = reset($numbers);
        $startValue = $previousValue;
        foreach ($numbers as $value) {
            if ($value > $previousValue + 1) {
                if ($startValue == $previousValue) {
                    $result[] = $startValue;
                } else {        
                    $result[] = $startValue . '-' . $previousValue;
                }
                $startValue = $value;
            }
            $previousValue = $value;
        }
        if ($startValue == $previousValue) {
            $result[] = $startValue;
        } else {        
            $result[] = $startValue . '-' . $previousValue;
        }
        return implode(',', $result);
    }
    
    /*
     * Creates an array with values the three letter representation for days of the 
     * week and keys the corresponding numeric representation.
     *
     * @return array
     */
    function createLookupNumberToDay() {
        $date = strtotime('now');
        $lookup = array();
        for ($i = 1; $i <= 7; $i++) {
            $lookup[date('w', $date)] = date('D', $date);
            $date = strtotime('+1 day', $date);
        }
        return $lookup;
    }
    
    /*
     * Converts a string listing days separated by commas into 
     * an array with values the numeric value for the corresponding
     * day of the week.
     *
     * @param string $days
     * @return array
     */
    function convertDaysToNumbers($days) {
        $result = array();
        $daysArray = explode(",", str_replace(" ","",$days));
        foreach ($daysArray as $day) {
            $result[] = date('w', strtotime($day));
        }
        return $result;
    }
    
    /*
     * Converts the numbers in a string to the corresponding 3-letter day of the
     * week abbreviation.
     *
     * @param string $string
     * @return string
     */
    function convertNumbersToDays($string) {
        $lookup = createLookupNumberToDay();
        return str_replace(array_keys($lookup), $lookup, $string);
    }
    
    function convert($string) {
        return (convertNumbersToDays(compressNumbers(convertDaysToNumbers($string))));
    }
    
    echo convert('mon,tue,wed,thu,fri,sat');
    echo '<br />';
    echo convert('mon,tue,wed,sat');
    echo '<br />';
    
    /**
    *将数字数组转换为包含范围和单个值的字符串
    *@param array$numbers一组数字
    *@返回字符串
    */
    函数压缩数($numbers){
    $result=array();
    排序(数字);
    $previousValue=重置($number);
    $startValue=$previousValue;
    foreach(数字为$value){
    如果($value>$previousValue+1){
    如果($startValue==$previousValue){
    $result[]=$startValue;
    }否则{
    $result[]=$startValue.'-'.$previousValue;
    }
    $startValue=$value;
    }
    $previousValue=$value;
    }
    如果($startValue==$previousValue){
    $result[]=$startValue;
    }否则{
    $result[]=$startValue.'-'.$previousValue;
    }
    返回内爆(“,”,$result);
    }
    /*
    *创建一个数组,该数组的值以三个字母表示,表示一天中的天数
    *week并为相应的数字表示形式设置关键帧。
    *
    *@return数组
    */
    函数createLookupNumberToDay(){
    $date=strottime('now');
    $lookup=array();
    
    对于($i=1;$i是否也需要在本周进行包装?例如,如果输入像fri、sat、sun、mon这样的内容,您希望输入fri-mon还是fri-sun、sun-mon?嗯,本周包装不是必需的。我想这是一个不错的选择,但本项目不需要。是否也需要在本周进行包装?例如,如果输入像fri、sat、sun、mon这样的内容,您希望输入fri-mon吗i-mon或fri-sun,sun-mon?嗯,这周的包装不是必需的。我想这将是一个很好的选择,但这个项目不需要…只是返回了我给它的相同字符串?+1表示努力…只是返回了我给它的相同字符串?+1表示努力我在搜索“网络”时找到了你的CompressNumber函数,它工作起来很有魅力nks!我在搜索“网络”时发现了你的compressNumbers功能,它工作起来很有魅力!谢谢!