数组A.包含很多项
    [0] => 01 11 02 03
    [1] => 01 11 02 04
    [2] => 01 11 02 14
    [3] => 01 11 02 05
    [4] => 01 11 02 15
    [5] => 01 11 02 06
    [6] => 01 11 02 07
    [7] => 01 11 03 04
    [8] => 01 11 03 14
    [9] => 01 11 03 05
 ...需要核对每项里的内容是否每个数字都出现在 某个string 内 比如C= 01 11 03 有什么最快的方法?例如判断 [9] => 01 11 03 05 /
 01 是否出现在 c内 
11 是否出现在 c 内 
如果有一项不存在 那么返回false.1.遍历 数组A 将每一项内容 转换成一个数组 B
2.循环B 进行 strpos/strstr 函数的判断是否存在 C(string) 内.如果其中一项不存在那么直接break;
如果循环到最后都是true 那么将该项内容复制给 新的数组 $test=array('01 11 02 03','01 11 02 04','01 11 02 14','01 11 02 05','01 11 02 15');
$c='01 11';
$good=array();for ($i=0;$i<sizeof ($test);$i++)
{
$temp=array();
$temp=explode(' ',$test[$i]);
for ($j=0;$j<sizeof($temp);$j++)
{
if (strstr($c,$temp[$j])=='')
{
break;
//不知道咋写了...怂了
}else
{

} }
}print_r($good);这样很慢吧?有更快的思路嘛?

解决方案 »

  1.   

    速度是不是更快不一定,但有个正则的方案,代码会比较简短。
    $test=array('01 11 02 03','01 11 02 04','01 11 02 14','01 11 02 05','01 11 02 15');
    $c='01 11';
    foreach(explode(' ',$c) as $v)
    {
            $p[] = "(?=.*\b{$v}\b)";
    }
    print_r(preg_grep('#'.implode('',$p).'#',$test));
    按你那个做法,你可以将$c再按空格切成数组,然后计算其和$temp[$j]的交集个数是不是和$c切成的数组个数一致,有个函数array_intersect,你可以看一下。
      

  2.   


    $test    =    array('01 11 02 03','01 11 02 04','01 11 02 14','01 11 02 05','01 11 02 15');
    $c       =    '01 11 02 03 04 05';
    $c_tmp   =     explode(' ', $c);
    $c_tmp   =     array_fill_keys($c_tmp, 1);
    $result  =     array();
    foreach($test as $tmp) {
        foreach(explode(' ', $tmp) as $ele)
            if(!isset($c_tmp[$ele])) continue 2;
        $result[] = $tmp;
    }
    print_r($result);
    /*------------输出结果---------------*/
    /*
     * Array
     * (
     *     [0] => 01 11 02 03
     *     [1] => 01 11 02 04
     *     [2] => 01 11 02 05
     * )
     */
      

  3.   

    请问 continue 2; 是啥意思咧?
      

  4.   

    Note: Note that unlike some other languages, the continue statement applies to switch and acts similar to break. If you have a switch inside a loop and wish to continue to the next iteration of the outer loop, use continue 2. 
      

  5.   

    php手册 -> 语言参考(language reference) -> 控制结构(constrol structures) -> continue/**
     * continue接收一个可选数值型参数, 用来表明它要跳出多少级的循环继续执行.
     * 注意: continu 0;和continue 1;都等价于continue; 跳出当前循环.
     */