Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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-组合循环的结果_Php_Loops_Merge - Fatal编程技术网

php-组合循环的结果

php-组合循环的结果,php,loops,merge,Php,Loops,Merge,我有这样的代码 error_reporting(E_ALL ^ (E_NOTICE | E_WARNING)); function multiexplode ($delimiters,$string) { $ready = str_replace($delimiters, $delimiters[0], $string); $launch = explode($delimiters[

我有这样的代码

            error_reporting(E_ALL ^ (E_NOTICE | E_WARNING)); 
            function multiexplode ($delimiters,$string) {
                $ready = str_replace($delimiters, $delimiters[0], $string);
                $launch = explode($delimiters[0], $ready);
                return  $launch;
            }
            function my_replace($srch, $replace, $subject, $skip=1){
                $subject = explode($srch, $subject.' ', $skip+1);
                $subject[$skip] = str_replace($srch, $replace, $subject[$skip]);
                while (($tmp = array_pop($subject)) == '');
                $subject[]=$tmp;
                return implode($srch, $subject);
            }
            function replace_element($str,$search,$replace,$num) {
                $num = $num - 1;
                $pieces = explode(',',$str);
                if($pieces[$num] == $search) {
                    $pieces[$num] = $replace;
                }
                return implode(',',$pieces);
            } 
            function returnFormat($template, $subject){
                preg_match_all("/\|/", $template, $matches, PREG_OFFSET_CAPTURE);
                foreach($matches[0] as $v) $subject = substr_replace($subject, "|", $v[1], 1);
                return $subject;
                }   

                $replace = "|";
                $pattern = "\|";

            $string = "101131110|101113110|"; 
            $data = "1020,0000,2022,1023,1024,1025,1024,1025,0000|1020,0000,2022,1023,1027,1025,1024,1025,0000|";
                $char = '3';
                $string = str_replace('|', '', $string);    $positions = array();
                $pos = -1; $b=0; 
                while (($pos = strpos($string, $char, $pos+1)) !== false) {{
                    $positions[] = $pos+1; }

                $result2 = implode(',', $positions);
                $res = explode(',',$result2); 
                $exp = multiexplode(array(",","|"),$data);
                $eyz= my_replace('|', ',', $data, 0);       
                $z=1; $y=1;
                foreach ($res as $val) {
                    $res2[$z]=$val; 
                    $valz = $res2[$z];
                    $z++; 
                    }
                        foreach ($exp as $value) {
                        $exp2[$y]=$value; 
                        $vals = $exp2[$valz];
                        $y++; 
                        } 

                        $items = explode(',',$eyz); 
                        $occurences = array_count_values($items);   
                        $st=$occurences[$vals];
                        $fix=replace_element($eyz,$vals,'JJJJ',$valz);

                preg_match_all("/$pattern/", $data, $matches, PREG_OFFSET_CAPTURE);
                    foreach($matches[0] as $v) $fix = substr_replace($fix, $replace, $v[1], 1);
                    echo "<br/>RESULT : <br/><font color='green'>".$fix."</font>";      
                        $b++;
                }
错误报告(E_ALL^(E_通知| E_警告));
函数multiexplode($delimiters,$string){
$ready=str_replace($delimiters,$delimiters[0],$string);
$launch=explode($delimiters[0],$ready);
返回$launch;
}
函数my_replace($srch,$replace,$subject,$skip=1){
$subject=explode($srch,$subject.'',$skip+1);
$subject[$skip]=str_replace($srch,$replace,$subject[$skip]);
而($tmp=array_pop($subject))='';
$subject[]=$tmp;
返回内爆($srch,$subject);
}
函数替换元素($str,$search,$replace,$num){
$num=$num-1;
$pieces=爆炸(“,”,$str);
如果($pieces[$num]==$search){
$pieces[$num]=$replace;
}
返回内爆(“,”,$pieces);
} 
函数返回格式($template,$subject){
preg\u match\u all(“/\\124;/”,$template,$matches,preg\u OFFSET\u CAPTURE);
foreach($v匹配[0])$subject=substr_replace($subject,“|,$v[1],1);
返回$subject;
}   
$replace=“|”;
$pattern=“\\\\”;
$string=“101131110 | 101113110 |”;
$data=“1020000022102310241025102410250000 | 1020000022102310271025102410250000 |”;
$char='3';
$string=str|u replace(“|”),“$string”$位置=数组();
$pos=-1$b=0;
while(($pos=strpos($string,$char,$pos+1))!==false){{
$positions[]=$pos+1;}
$result2=内爆(“,”,$positions);
$res=分解(“,”,$result2);
$exp=multiexplode(数组(“,”,“|”)$data);
$eyz=my|u replace(“|”),“,”,$data,0);
$z=1;$y=1;
foreach($res作为$val){
$res2[$z]=$val;
$valz=$res2[$z];
$z++;
}
foreach($exp作为$value){
$exp2[$y]=$value;
$VAL=$exp2[$valz];
$y++;
} 
$items=分解(“,”,$eyz);
$occurrences=数组\计数\值($items);
$st=$occurrences[$VAL];
$fix=替换_元素($eyz、$VAL、'JJJ',$valz);
preg_match_all(“/$pattern/”,$data,$matches,preg_OFFSET_CAPTURE);
foreach($0]匹配为$v)$fix=substr\u replace($fix,$replace,$v[1],1);
echo“
结果:
“$fix.”; $b++; }
我有一个这样的结果

            error_reporting(E_ALL ^ (E_NOTICE | E_WARNING)); 
            function multiexplode ($delimiters,$string) {
                $ready = str_replace($delimiters, $delimiters[0], $string);
                $launch = explode($delimiters[0], $ready);
                return  $launch;
            }
            function my_replace($srch, $replace, $subject, $skip=1){
                $subject = explode($srch, $subject.' ', $skip+1);
                $subject[$skip] = str_replace($srch, $replace, $subject[$skip]);
                while (($tmp = array_pop($subject)) == '');
                $subject[]=$tmp;
                return implode($srch, $subject);
            }
            function replace_element($str,$search,$replace,$num) {
                $num = $num - 1;
                $pieces = explode(',',$str);
                if($pieces[$num] == $search) {
                    $pieces[$num] = $replace;
                }
                return implode(',',$pieces);
            } 
            function returnFormat($template, $subject){
                preg_match_all("/\|/", $template, $matches, PREG_OFFSET_CAPTURE);
                foreach($matches[0] as $v) $subject = substr_replace($subject, "|", $v[1], 1);
                return $subject;
                }   

                $replace = "|";
                $pattern = "\|";

            $string = "101131110|101113110|"; 
            $data = "1020,0000,2022,1023,1024,1025,1024,1025,0000|1020,0000,2022,1023,1027,1025,1024,1025,0000|";
                $char = '3';
                $string = str_replace('|', '', $string);    $positions = array();
                $pos = -1; $b=0; 
                while (($pos = strpos($string, $char, $pos+1)) !== false) {{
                    $positions[] = $pos+1; }

                $result2 = implode(',', $positions);
                $res = explode(',',$result2); 
                $exp = multiexplode(array(",","|"),$data);
                $eyz= my_replace('|', ',', $data, 0);       
                $z=1; $y=1;
                foreach ($res as $val) {
                    $res2[$z]=$val; 
                    $valz = $res2[$z];
                    $z++; 
                    }
                        foreach ($exp as $value) {
                        $exp2[$y]=$value; 
                        $vals = $exp2[$valz];
                        $y++; 
                        } 

                        $items = explode(',',$eyz); 
                        $occurences = array_count_values($items);   
                        $st=$occurences[$vals];
                        $fix=replace_element($eyz,$vals,'JJJJ',$valz);

                preg_match_all("/$pattern/", $data, $matches, PREG_OFFSET_CAPTURE);
                    foreach($matches[0] as $v) $fix = substr_replace($fix, $replace, $v[1], 1);
                    echo "<br/>RESULT : <br/><font color='green'>".$fix."</font>";      
                        $b++;
                }
结果1: 1020000020221023,JJJJ,1025102410250000 | 102000002022102310271025102410250000| 结果2: 102000002022102310241025102410250000 | 10200000202210231027,JJJJ,102410250000|

如何组合循环的结果,因此有如下输出:

10200000221023,JJJJ,1025102410250000 | 102000002210231027,JJJJ,102410250000|

提前谢谢

关于,

试试这个:

preg_match_all("/$pattern/", $data, $matches, PREG_OFFSET_CAPTURE);
    foreach($matches[0] as $v) $fix = substr_replace($fix, $replace, $v[1], 1);
        $arrayToCombine[] = explode(",", $fix);
    $b++;
}
array_replace( $arrayToCombine[0], $arrayToCombine[1] );
但我必须问:在您的示例中,有两个结果,大约有16个值用逗号分隔。你给我们看的组合输出也有17个用逗号分隔的值。。。是否要删除组合阵列中的重复阵列

array_unique( array_merge( $arrayToCombine[0], $arrayToCombine[1] ) )
更新:尝试以下操作:

preg_match_all("/$pattern/", $data, $matches, PREG_OFFSET_CAPTURE);
    foreach($matches[0] as $v) $fix = substr_replace($fix, $replace, $v[1], 1);
        $arrayToCombine[] = multiexplode([","], $fix);
    $b++;
}
function combine_arrays( $array1, $array2 )
{
    $size = max( count($array1), count($array2) );
    $data = array();
    // I see now, there is the final: add precedence to the 'JJJJ'
    for ($i=0; $i < $size; $i++) { 
        $thisVal = ( $array1[$i] == 'JJJJ' OR $array2[$i] == 'JJJJ' ) ? 'JJJJ' : $array1[$i];
        $data[] = $thisVal;
    }       

    return $data;
}
print_r(combine_arrays( $arrayToCombine[0], $arrayToCombine[1] ));
echo(implode(",",combine_arrays( $arrayToCombine[0], $arrayToCombine[1] )));
preg_match_all(“/$pattern/”,$data,$matches,preg_OFFSET_CAPTURE);
foreach($0]匹配为$v)$fix=substr\u replace($fix,$replace,$v[1],1);
$arrayToCombine[]=多重分解([“,”],$fix);
$b++;
}
函数combine_数组($array1,$array2)
{
$size=max(count($array1)、count($array2));
$data=array();
//我现在明白了,最后一个问题是:给“jj”加上优先级
对于($i=0;$i<$size;$i++){
$thisVal=($array1[$i]='JJJJ'或$array2[$i]='JJJJ')?'JJJJ':$array1[$i];
$data[]=$thisVal;
}       
返回$data;
}
打印(合并数组($arrayToCombine[0],$arrayToCombine[1]);
回波(内爆(“,”,组合_数组($arrayToCombine[0],$arrayToCombine[1]));

我不想删除任何内容,在上述情况下,我希望JJJ合并到相同的位置。我已尝试了您的语法,但未按上述方式显示输出,您能否再次告诉我,谢谢您现在检查是否有JJJ并进行合并。。。也从爆炸中移除了|。看起来几乎完美,但插入了1020000020221023,JJJJ,10241025102410250000 | 10200000202210231027,10‌​25,JJJJ,102410250000 |好的axel,我将等待更新,非常感谢您的时间和帮助