2013-04-28 64 views
0

的結果我有這樣PHP - 結合循環

  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 |

在此先感謝

問候,

回答

0

試試這個:

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

UPDATE:試試這個:

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

我不想除去上面的任何東西,在的情況下,我想JJJJ在相同的位置 – ivanichi 2013-04-28 23:41:57

+0

合併我已經試過你的語法,但不顯示輸出如上,你能再次告訴我,在此先感謝 – ivanichi 2013-04-28 23:55:59

+0

現在檢查是否有一個JJJJ,並進行合併...也刪除|從爆炸。 – 2013-04-28 23:59:02