2015-11-23 52 views
2

我想爆炸一個數組,但有一點區別。我想合併間隔-標誌。如何才能做到這一點? (對數組進行排序!)Implode按逗號和合並間隔將數字數組排序爲字符串

例子:

array(1,2,3,6,8,9) => "1-3,6,8-9" 
array(2,4,5,6,8,10) => "2,4-6,8,10" 
+2

沒有PHP函數這可以爲你做到這一點。 您需要編寫自己的腳本。 – KiwiJuicer

+1

就像@Kiwi Juicer說的那樣。如果你花一些時間思考它,你可以簡單地用foreach()循環來做到這一點。考慮您需要執行哪些步驟來檢測和合並間隔。 – Andrea

回答

8

這應該爲你工作:

首先每次迭代,我們簡單地迭代的當前數量追加到$result串:

$result .= $arr[$i]; 

之後,我們檢查一個while循環,如果存在下一個elemen t在數組(1)中,並且它跟隨當前迭代(2)的數量。我們這樣做,直到條件的計算結果爲假:

//(1)Check if next element exists  (2)Check if next element follows up the prev one 
     ┌───────┴───────┐ ┌───────────┴────────────┐  
while(isset($arr[$i+1]) && $arr[$i] + 1 == $arr[$i+1] && ++$range) 
    $i++; 

然後我們檢查,如果我們有一個範圍(例如1-3)與否。如果是的話那麼我們追加破折號和範圍的結束編號到結果字符串:

if($range) 
    $result .= "-" . $arr[$i]; 

最後,我們還要檢查,如果我們在數組的結尾,並不需要附加一個逗號了:

if($i+1 < $l) 
    $result .= ","; 

代碼:

<?php 

    $arr = array(1,2,3,6,8,9); 
    $result = ""; 
    $range = 0; 

    for($i = 0, $l = count($arr); $i < $l; $i++){ 

     $result .= $arr[$i]; 

     while(isset($arr[$i+1]) && $arr[$i] + 1 == $arr[$i+1] && ++$range) 
      $i++; 

     if($range) 
      $result .= "-" . $arr[$i]; 

     if($i+1 < $l) 
      $result .= ","; 

     $range = 0; 

    } 

    echo $result; 

?> 

輸出:

1-3,6,8-9 
2
$oldArray=array(2,4,5,6,8,10); 

    $newArray=array(); 


    foreach($oldArray as $count=>$val){ 
     if($count==0){ 
      //begin sequencing 
      $sequenceStart=$sequenceEnd=$val; 
     } 

     if($val==$sequenceEnd+1){ 
      $sequenceEnd=$val; 
      continue; 
     }else{ 
      if($sequenceEnd==$val){ 
       //do nothing 
       continue; 
      } 


     } 

     //new sequence begins 
     //save new sequence 
     if($sequenceStart==$sequenceEnd){ 
      //sequnce is a single number 
      $newArray[]=$sequenceEnd; 
     }else{ 
      $newArray[]=$sequenceStart.'-'.$sequenceEnd; 
     } 

     //reset sequence 
     $sequenceStart=$sequenceEnd=$val; 
    } 

    //new sequence begins 
    //save new sequence 
    if($sequenceStart==$sequenceEnd){ 
     //sequnce is a single number 
     $newArray[]=$sequenceEnd; 
    }else{ 
     $newArray[]=$sequenceStart.'-'.$sequenceEnd; 
    } 

    //reset sequence 
    $sequenceStart=$sequenceEnd=$val; 



    return implode(',', $newArray); 
+2

儘管這段代碼可能解決這個問題,但代碼中的[包括解釋](http://meta.stackexchange.com/questions/114762/explaining-entirely- code-based-answers)確實有助於提高您的帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。也請儘量不要用解釋性註釋來擠佔代碼,這會降低代碼和解釋的可讀性! – Rizier123

0

沒有這樣的功能,因此您需要自己創建一個。我剛剛創建了一個樣本函數如何可能看起來像,也有這麼多的可能的解決方案(沒有嘗試,如果它的實際工作,因爲我沒有在到達ATM網絡服務器)

<?php 
function implodeNumberArray($arr) { 
$lastValue = NULL; 
$o = ""; 
//For each value in array 
foreach ($arr as $v) { 
    if(!is_null($lastValue)) { 
    //If the number is following, do not paste it 
    if(($lastValue+1) == $v) { 
    //Check if the - sign was already posted 
    if(!(stripos(strrev($o), '-') === 0)) { 
    // - sign not pasted, therefore paste it 
    $o .= "-"; 
    } 
    } else { 
    //Check if there is a - sign at the end 
    if((stripos(strrev($o), '-') === 0)) { 
    // Has - => paste 'prevValue,value'' 
    $o .= $lastValue . "," . $v; 
    } else { 
    //Check if there is a , sign at the end 
    if((stripos(strrev($o), ',') === 0)) { 
     // No - but , => paste 'value' 
     $o .= $v; 
    } else { 
     // No - and no , => paste ',value' 
     $o .= ",".$v; 
    }   
    } 
    } 
    } else { 
    $o = $v; 
    } 

    $lastValue = $v; 
} 
//Check if the implode has the last number set correctly 
if((stripos(strrev($o), '-') === 0)) { 
    $o .= $lastValue; 
} 
return $o; 
} 

echo implodeNumberArray(array(1,2,3,6,8,9)); 
?> 
+2

1)你在你的代碼中有一個錯字2)不是它不工作(誰曾經高舉它,我不知道爲什麼) – Rizier123

+0

對不起,我糾正了這個函數,現在它的工作原理應該如此 – Xavjer

相關問題