2014-07-24 67 views
0

我有這個數組,我希望數不斷重複值。但它並沒有給我想要的東西。我已經用一個例子解釋了我的需求。所以如果你有任何關於如何解決此問題。如何獲得連續數組中連續重複值的計數?

Array 
    (
     [0] => Array 
      (
       [0] => 44 
       [1] => 1132 
      ) 

     [1] => Array 
      (
       [0] => 27 
       [1] => 28 
       [2] => 32 
       [3] => 37 
       [4] => 38 
       [5] => 114 
       [6] => 117 
       [7] => 273 
       [8] => 1132 
      ) 

     [2] => Array 
      (
       [0] => 27 
       [1] => 28 
       [2] => 32 
       [3] => 34 
       [4] => 36 
       [5] => 37 
       [6] => 38 
       [7] => 44 
       [8] => 114 
       [9] => 117 
       [10] => 273 
      ) 

     [3] => Array 
      (
       [0] => 27 
       [1] => 28 
       [2] => 32 
       [3] => 34 
       [4] => 36 
       [5] => 37 
       [6] => 38 
       [7] => 44 
       [8] => 114 
       [9] => 117 
       [10] => 273 
      ) 
    ) 

我想導致這樣的:

array 
    (
     [44]=>1 
     [1132]=>2 
     [27]=>3 
     [28]=>3 
     [32]=>3 
    ........ 
     [273]=>2 
     [1132]=>1 
    ) 

我這樣做的方式,但它不是讓結果我想..它也可以是用戶定義的。如果用戶輸入數字「3」,應持續重複3次或更多次的值。

$s=array(); 
for($k=0;$k<count($absentNoArray);$k++) 
{ 
    for($p=0;$p<count($absentNoArray[$k]);$p++) 
    { 
     $temp=$absentNoArray[$k][$p]; 
     $count=1; 
     for($l=$k+1;$l<count($absentNoArray);$l++) 
     {   
      for($q=0;$q<count($absentNoArray[$l]);$q++) 
      { 
       if($temp==$absentNoArray[$l][$q]) 
       { 
        $count++; 
       } 
       else 
       { 
        break; 
       }    
      } 
     } 
     $s[]=$count; 
    } 
} 
print_r($s); 

預先感謝您..

+0

'我這樣做了,但是它給了我想要的結果......所以它工作與否? –

+0

遞歸有多少可能? – serakfalcon

+0

我不明白你想做什麼... –

回答

0

如果你想在陣列不同的值的直線數,遞歸的任何級別:

function array_val_counter($input) { 
    $response = array(); 
    if (is_array($input)) { 
     array_walk($input,function($value,$key) { 
      if (is_array($value)) { 
       $recursiveresponse = array_val_counter($value); 
       foreach($recursiveresponse as $recurval => $count) { 
        if (isset($response[$recurval])) { 
         $response[$recurval]+=$count; 
        } else { 
         $response[$recurval]=$count; 
        } 
       } 
      } elseif (isset($response[$value])) { 
       $response[$value]++; 
      } else { 
       $response[$value] = 1; 
      } 
     }); 
    } else { 
     //input is not an array, do some error 
    } 
    return $response; 
} 

編輯:對數字的計數順序:

function array_val_in_seq($input) { 
    $result = array(); 
    $numarrays = count($input); 
    for($i=0;$<$numarrays;$i++) { 
     switch($i) { 
      case 0: 
       //for the first array, add every value to the results 
       //and go through the remaining arrays, stopping if the result is not found 
       //store how far it got before stopping (aka consecutive count) 
       foreach($input[0] as $arrayval) { 
        for($y=1;$y<$numarrays;$y++) { 
         $result[$arrayval] = 1; 
         if(in_array($arrayval,$input[$y])) { 
          $result[$arrayval] = $y+1; 
         } else { 
          break; 
         } 
        } 
       } 
       break; 
      case $numarrays: 
       //for the last array, give any elements that do not already exist 
       // a count of 1 
       foreach($input[$numarrays] as $arrayval) { 
        if(array_key_exists($arrayval,$result) === false) { 
         $result[$arrayval] = 1; 
        } 
       } 
       break; 
      default: 
       //if the array value doesn't exist in the previous array (not consecutive) go through the remaining arrays, stopping if the value isn't found. 
       //if the number is larger than what already exists, replace it. 
       foreach($input[$i] as $arrayval) { 
        if(!in_array($arrayval,$input[$i-1])) { 
         for($y=$i+1;$y<$numarrays;$y++) { 
          if(in_array($arrayval,$input[$y])) { 
           $count = $y - $i + 1; 
           if (!isset($result[$arrayval] || 
           (isset($result[$arrayval]) && $result[$arrayval] > $count)) { 
            $result[$arrayval] =$count; 
           } 
          } else { 
           break; 
          } 
         } 
        } 
       } 
     } 
    } 
} 
+0

我不想直接計數,實際上需要連續計數。 – user3740116

1

試試這個

$myArray = []; //This is your Array 
$combinedArr = []; 

foreach ($myArray as $item) { 
    array_push($combinedArr, $item); 
} 

$quantityArray = array_count_values($combinedArr); 
print_r($quantityArray) 

http://php.net/manual/en/function.array-count-values.php

+0

是的,這是我正在考慮的確切答案,但它與OP所需的輸出不匹配。很好的答案,但這個問題沒有多大意義。 –

0

我不知道該怎麼好這個執行,但它爲我工作:

function countConsecutiveValues($arrays, $minCount = 1) { 
    // Merge all values into a single array 
    $values = array_unique(call_user_func_array('array_merge', $arrays), SORT_REGULAR); 
    $counts = array(); 
    // Loop through each value 
    foreach ($values as $value) { 
    $count = 0; 
    $counts[$value] = 0; 
    foreach ($arrays as $array) { 
     if (array_search($value, $array)) { 
     // If the current array contains the value, increment the counter 
     $count++; 
     } 
     else { 
     // If the current array does not contain the value, update the counts 
     // array (if necessary) and reset the counter 
     if ($count >= $minCount) { 
      $counts[$value] = max($counts[$value], $count); 
     } 
     $count = 0; 
     } 
    } 
    // Get the final count after the last array is processed 
    if ($count >= $minCount) { 
     $counts[$value] = max($counts[$value], $count); 
    } 
    } 
    // This should remove any zero counts before returning 
    return array_filter($counts); 
} 

當像這樣使用:

$arrays = array(
    array(44, 1132), 
    array(27, 28, 32, 37, 38, 114, 117, 273, 1132), 
    array(27, 28, 32, 34, 36, 37, 38, 44, 114, 117, 273), 
    array(27, 28, 32, 34, 36, 37, 38, 44, 114, 117, 273) 
); 

print_r(countConsecutiveValues($arrays, 3)); 

輸出是:

Array ([27] => 3 [28] => 3 [32] => 3 [37] => 3 [38] => 3 [114] => 3 [117] => 3 [273] => 3) 
+0

heya thanx ..我也解決了。無論如何感謝你的努力。 – user3740116

0

我得到了答案,但非常感謝你的努力。

$userValue=4; 
$idCount=array(); 
$s=array(); 
$status=0; 
for($k=0;$k<count($absentNoArray);$k++) 
{ 
    for($p=0;$p<count($absentNoArray[$k]);$p++) 
    { 
     $temp=$absentNoArray[$k][$p]; 
     $count=1; 
     for($l=$k+1;$l<count($absentNoArray);$l++) 
     {   
      foreach($absentNoArray[$l] as $key=>$val) 
      { 
       if($temp==$val) 
       {     
        $count++; 
        $status=1; 
       }  
      } 
      if($status==0) 
      { 
       break; 
      } 
      else 
      { 
       if($count<$userValue) 
       { 
        $status=0; 
        continue; 
       } 
       else 
       { 
        if(!in_array($temp,$s)) 
        { 
         $s[]=$temp; 
         break; 
        } 
       } 
      } 
     } 
    }  
} 
print_r($s);