溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP關(guān)于時(shí)間的時(shí)段的重合、 整合的方法

發(fā)布時(shí)間:2020-08-11 16:43:35 來(lái)源:網(wǎng)絡(luò) 閱讀:453 作者:jimwoooo 欄目:web開(kāi)發(fā)

<html >

<title></title>

<body>

<div>

 

<?php

     /**

     * 獲取到兩個(gè)重合時(shí)段的最大和最小

     * [get_min_max description]

     * @author jimswoo 20161016 <[<email address>]> 

     * @param  [type] $a [description]

     * @param  [type] $b [description]

     * @return [type]    [description]

     */

     function get_min_max($a,$b){

        $sort = array_merge($a,$b);

        array_multisort($sort);

        $end = array_pop($sort);

        return $sort[0].'#'.$end;//array($sort[0],$end);

    }


    /**

     * [is_repeat description]

     * 是否兩個(gè)時(shí)段重合

     * @author jimswoo 20161016 <[<email address>]> 

     * @param  [type]  $target  [description]

     * @param  [type]  $compare [description]

     * @param  boolean $run     [description]

     * @return boolean          [description]

     */

     function is_repeat($target,$compare,$run = true){

        $min = $compare[0];

        $max = $compare[1];

        $res = false;

        foreach($target as $v){

            if(($v >= $min && $v <= $max)) {

                $res = true;

                break;

            }

        }

        if($run && !$res){

          $res = is_repeat($compare,$target,false);

        }

        return $res;

    }


    /**

     * 把時(shí)段的值從字符串轉(zhuǎn)為數(shù)組

     * @author jimswoo 20161016 <[<email address>]> 

     * [changeValue description]

     * @param  [type] $val [description]

     * @return [type]      [description]

     */

     function changeValue($val){

        $val = array_unique($val);

        $list = array();

        foreach($val as $v){

            $list[] = explode('#',$v);

        }

        return $list;

    }


    /**

     * [main_run description]

     * 比較方法

     * @author jimswoo 20161016 <[<email address>]> 

     * @param  [type] $all [description]

     * @return [type]      [description]

     */

     function main_run($all){

        $leng = count($all);

        $result = $un = array();

        $count = 0;

       for($i = 0;$i<$leng;$i++){

        for($j = $leng - 1;$j >= $i;$j--){

            if(is_repeat($all[$j],$all[$i])){

                if($j != $i){

                    $count++;

                }else{

                    $un[] = $all[$i][0].'#'.$all[$i][1];

                }

                $result[] = get_min_max($all[$j],$all[$i]);

                $all[$i] = $all[$j] = array(-3,-2);

                

            }else{

                $un[] = $all[$i][0].'#'.$all[$i][1];

            }

         }

          

        }

      

        $result = array_merge($result,$un);

      

        if($count == 0){

           $result = $all;

        }

        return array('c'=>$count,'v'=>$result);

    }


    /**

     * [getComfirmTimes description]

     * @author jimswoo 20161016 <[<email address>]> 

     * @param  [type] $all [description] 格式array(array(2,4),array(34,332))

     * @return [type]      [description]

     */

     function getComfirmTimes($all){

        if(empty($all)){

            return array();

        }

        $c=0;

        do{

          $is_end = main_run($all);

          if($is_end['c'] != 0){

              //var_dump($all);

              $all = changeValue($is_end['v']);

              

          }else{

             

              foreach($all as $k=>$v){

                  if($v[0] == -3){

                      unset($all[$k]);

                  }

              }

          }

          $c++;

        }while($is_end['c'] != 0 && $c < 120);

        return $all;

    }


    $test = array(

    array(2,6),

    array(5,9),

    array(10,11),

    array(15,20),

    array(22,23),

    array(13,19)

    );

    $res = getComfirmTimes($test);

    print_r($res);

?>

</div>

</body>

</html>


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI