2017-05-02 52 views
0

我有這樣數組值:拆分PHP陣列組時值範圍或沒有

$src = array(3,15,16,17,18,20,25,7); 

如何我可以將它分成:

array(
    [0] => 3, 
    [1] => array([0] => 15, [1] => 18),//when value is range get start and end 
    [2] => 20, 
    [3] => 25, 
    [4] => 
); 
+0

我不理解的問題。你能重申一下這個問題嗎? – RepeaterCreeper

+0

這個問題還不清楚。詳細說明你的問題 – RomanPerekhrest

+0

基本上,他問的是...如果輸入是1,2,3,4,5,他希望它是'[0] => 1,[1] => 5'。我的問題是,如果你有一個情況,你的序列是'1,3,4,20',你希望它是'[0] => 3,[1] => 4'還是隻有'3,4' ? – Dimi

回答

1

檢查該代碼。如果您的序列/範圍差異僅基於下一個數字,那麼此代碼將適用於您。

<?php 
$src = array(3,15,16,17,18,20,25,7); 
$tot = count($src); 
$resarr = array(); 
$st = $ed = ''; 
for($i=0;$i<$tot;$i++){ 
    if($st==''){ 
     if(isset($src[$i+1]) && $src[$i+1] == $src[$i]+1){ 
      $st = $src[$i]; 
     } 
    } 
    else{ 
     if(!isset($src[$i+1]) || $src[$i+1] != $src[$i]+1){ 
      $ed=$src[$i]; 
     } 
    } 
    if($st=='' && $ed==''){ 
     $resarr[] = $src[$i]; 
    } 
    elseif($st!='' && $ed!=''){ 
     $resarr[] = array($st,$ed); 
     $st = $ed=''; 
    } 
} 
print_r($src); 
echo "<pre>";print_r($resarr);echo "</pre>"; 

?> 

另外phpfiddle的例子在這裏http://phpfiddle.org/lite/code/k7c4-wvdg。保持練習寫自己的邏輯。

1
// Make copy of array with shifting to one item. 
// And, to save array length, add one element to the end of the array. 
// It can be any number, but not continue sequence. 
// I add the last item of source array 
$src1 = $src; 
array_shift($src1); 
array_push($src1,end($src)); 

// Then subtract corresponding items of array 
$temp = array_map(function ($i, $j) { return $j-$i; }, $src, $src1); 
// Look at arrays 
// $src 3, 15, 16, 17, 18, 20, 25, 7 
// $temp 12, 1, 1, 1, 2, 5, -18, 0, 
// As you can see, all elements of sequences, but the last, has 1 in `temp` array. 
// Therefore, we just need to collect the result 
$sarr = false; 
$result = array(); 

for($i=0; $i<count($src); $i++) { 
    if($temp[$i] == 1) { 
     if(! $sarr) { 
     $sarr = $src[$i]; 
     } 
    } 
    else { 
    if(! $sarr) { 
     $result[] = $src[$i]; 
    } 
    else { 
     $result[] = [$sarr, $src[$i]]; 
     $sarr = false; 
    } 
    }   
} 

print_r($result); 

Test it there

0

我加了一些值只是爲了掩飾邊緣情況。評論解釋。

<?php 
$src = array(3,4,15,16,17,18,20,25,7,8); 

$res = []; 
$start = null; 

//Rather than make a counter use a for loop 
for($i=0; $i < count($src); $i++){ 
    //Make sure i+1 is not bigger than array 
    //If current index value + 1 
    //Euqals the next index value we have a range 
    if($i+1 < count($src) && $src[$i]+1 == $src[$i+1]){ 
     if($start === null){ 
      $start = $i; 
     } 
     //Once the range is over we can use the current index as end 
    } elseif($start !== null){ 
     $res[] = array($src[$start], $src[$i]); 
     $start = null; 
     $end = null; 
     //There was never a range. 
    } else { 
     $res[] = $src[$i]; 
    } 
} 


echo "<pre>"; 
print_r($res); 

結果:

Array 
(
    [0] => Array 
     (
      [0] => 3 
      [1] => 4 
     ) 

    [1] => Array 
     (
      [0] => 15 
      [1] => 18 
     ) 

    [2] => 20 
    [3] => 25 
    [4] => Array 
     (
      [0] => 7 
      [1] => 8 
     ) 

)