2011-12-22 29 views
2

假設我有以下數組$diffPHP搜索數組內循環

a, a, a, a, b, b, b, a, a, b, b, b, a, a, a, b 

A represents a value inside $diff. 
B represents an Array inside $diff. 

現在我都數不過來A,如果它發生在其序列more than two timesis not an Arrayinstead a value)。否則,無視它。

對於上面的輸入,代碼應該具有如下功能

[a] = not an array; 0 
[a,a] = not an array; 0 
[a,a,a] = not an array; 3 
[a,a,a,a] = not an array; 4 
[b] = array; 
[b,b] = array; 
[b,b,b] = array; 
[a] = not an array; 0 
[a,a] = not an array; 0 
[b] = array; 
[b,b] = array; 
[b,b,b] = array; 
[a] = not an array; 0 
[a,a] = not an array; 0 
[a,a,a] = not an array; 3 
[b] = array; 

這裏是我的嘗試,但它不工作!價值得到改變,因爲該值會被替換。

<?php 

foreach($diff as $key => $val) { 

    if (!is_array($diff[$key])) { // THIS MEANS THAT THE CURRENT ELEMENT IS NOT AN ARRAY. 
     if(is_array($diff[$key-1])) { //START OF SEQ. IF THE PREVIOUS ELEMENT IS AN ARRAY AND CURRENT ELEMENT IS NOT AN ARRAY. 

     $SEQ_START=$key; 
     $n=1; 

      for($i=0; $i<=count($diff); $i+=1) { // I AM CHECKING HERE IF THE NEXT 3 ELEMENTS are NOT ARRAY, HENCE I CAN INCREMENT IT 

      if(!is_array($diff[$SEQ_START+$i])) $n+=1; 
      else $n=0; 
      } 
     } 
    } 
} 

?> 
+0

我明白計數的邏輯,但是您要查找的最終結果是什麼?連續片段的數量(> 2)或連續片段中的元素數量(> 2)? (如果前者,答案是2,如果後者,答案是7) – Grexis 2011-12-22 10:06:58

+0

我需要在上面的例子中答案是7。 – SupaOden 2011-12-22 10:12:57

回答

2

修訂按照@Grexis'註釋低於

$diff = array(array(),'a', 'a', 'a', 'a', array(), array(), array(), 'a', 'a', array(), array(), array(), 'a', 'a', 'a', array()); 

// Counter to hold current sequence total 
$count = 0; 
// Array to hold results 
$counted = array(); 

// Loop array 
foreach ($diff as $key => $val) { 
    if (is_array($val)) { // If it is an array 
    if ($count > 2) { // If the counter is more than 2 
     $counted[(isset($seq)) ? $seq + 1 : 0] = $count; // add it to the array 
    } 
    // Reset the counter 
    $count = 0; 
    $seq = $key; 
    } else { 
    // Increment the counter 
    $count++; 
    } 
} 
// If there is a >2 counter at the end, add one more result 
if ($count > 2) { 
    $counted[(isset($seq)) ? $seq + 1 : 0] = $count; 
} 

print_r($counted); 
// Outputs: 
// Array 
// (
//  [0] => 4 
//  [12] => 3 
//) 

// or if you want the total count 
$total = array_sum($counted); 
echo $total; // 7 

See it working

+0

這是不必要的,但如果您在foreach中包含索引,則可以將序列開頭的索引作爲索引存儲在$ count中。類似於'$ count [$ index- $ count] = $ count;'在第10行和'$ counts [count($ diff) - $ count] = $ count;'在線21 – Grexis 2011-12-22 10:20:37

+0

@Grexis我更新了代碼我會怎麼做:-D – DaveRandom 2011-12-22 10:26:42

+0

不會說謊,你的實現絕對是整潔的... showoff:p – Grexis 2011-12-22 10:35:38

1

您只需要一個計數器來計算連續的非數組值。與每一個數組值增加它和與每一個非數組值重置:

$seqLength = 0; 
foreach ($arr as $index => $value) { 
    if (is_array($value)) { 
     $seqLength++; 
     echo 'array'; 
    } else { 
     $seqLength = 0; 
     echo 'not an array'; 
    } 
    if ($seqLength > 2) { 
     echo '; '.$seqLength; 
    } else { 
     echo '; 0'; 
    } 
} 
0

也許:

$a = array(1,1,1,1,1,array(1,1),1,1,1,1,1,array(1,1)); 
$i = 0; 
$j = 0; 
foreach ($a as $item){ 
    if ($i>=2){ 
     $j=$i + 1; 
    } 
    if (!is_array($item)){ 
     $i++; 
     echo $j.'<br>'; 
    }else{ 
     $i=0; 
     $j=0; 
     echo '<br>'; 
    } 
} 

輸出:

0 
0 
3 
4 
5 

0 
0 
3 
4 
5