解决方案 »

  1.   

    是这个意思?$dict = array( 8, '6-9', '5,8,9');
    $in = '5-7';$d = fmt($dict);
    $f = array_filter($d, function($ar) use ($in) {
      $in = fmt($in);
      return array_intersect(current($in), $ar);
    });print_r(array_intersect_key($dict, $f));function fmt($ar) {
      if(! is_array($ar)) $ar = array($ar);
      foreach($ar as $r) {
        $t = preg_split('/[-,]/', $r);
        $res[] = strpos($r, '-') ? range($t[0], $t[1]) : $t;
      }
      return $res;
    }
    Array
    (
        [1] => 6-9
        [2] => 5,8,9
    )
      

  2.   


    用了倒排索引   没有楼上的代码好看<?php$dict = array( 8, '6-9', '5,8,9');
    $in = '5-7';$invertIndex = array();foreach($dict as $index=>$item){    $numbers = preg_split('/[-,]/', $item);    if(strpos($item,'-')){       $numbers = range($numbers[0],$numbers[1]);
        }
        foreach($numbers as $number){        if(isset($invertIndex[$number])){
                $invertIndex[$number] = array($index);
            }else{            $invertIndex[$number][] = $index;
            }    }
    }$ins = preg_split('/[-,]/', $in);if(strpos($in,'-')){    $ins = range($ins[0],$ins[1]);
    }$indexs =array();foreach($ins as $item){    if(isset($invertIndex[$item])){
            $indexs = array_merge($indexs,$invertIndex[$item]);
        }
    }$indexs = array_unique($indexs);$ret = array();
    foreach($indexs as $index){    $ret[] = $dict[$index];
    }var_dump($ret);
    array(2) {
      [0]=>
      string(5) "5,8,9"
      [1]=>
      string(3) "6-9"
    }