【问题标题】:php - combining the results of loopingphp - 结合循环的结果
【发布时间】:2013-04-22 13:18:46
【问题描述】:

我有这样的代码

            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: 1020,0000,2022,1023,JJJJ,1025,1024,1025,0000|1020,0000,2022,1023,1027,1025,1024,1025,0000| 结果 2: 1020,0000,2022,1023,1024,1025,1024,1025,0000|1020,0000,2022,1023,1027,JJJJ,1024,1025,0000|

如何组合循环的结果,所以有这样的输出:

1020,0000,2022,1023,JJJJ,1025,1024,1025,0000|1020,0000,2022,1023,1027,JJJJ,1024,1025,0000|

提前致谢

问候,

【问题讨论】:

    标签: php loops merge


    【解决方案1】:

    试试这个:

    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] );
    

    但我要问:在您的示例中,有 2 个 结果,其中大约 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] )));
    

    【讨论】:

    • 我不想删除任何东西,在上述情况下,我希望 JJJJ 合并到同一位置
    • 我试过你的语法,但没有像上面那样显示输出,你能再告诉我一次吗,提前谢谢
    • 现在检查是否有 JJJJ 并进行合并...还删除了 |从爆炸中。
    • 似乎几乎完美,但是插入了 1020,0000,2022,1023,JJJJ,1024,1025,1024,1025,0000|1020,0000,2022,1023 ,1027,10‌​25,JJJJ,1024,1025,0000|
    • 好的 axel,我会等待更新,非常感谢您的时间和帮助
    猜你喜欢
    • 2012-10-09
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2015-10-05
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多