有點像一個蠻橫的方法。有點像kingkero引用。例如:
$array = array('one','one','one','five','five','five','five','five','four','four', 'one','four');
$prev = '';
$sequences = array();
// group them
foreach($array as $value) {
if(!isset($sequences[$value])) {
$sequences[$value][] = 0;
$prev = $value;
}
if($prev == $value) {
$val = array_pop($sequences[$value]) + 1;
$sequences[$value][] = $val;
} else {
$prev = $value;
$sequences[$value][] = 1;
}
}
序列將是這樣的:
Array
(
[one] => Array
(
[0] => 3
[1] => 1
)
[five] => Array
(
[0] => 5
)
[four] => Array
(
[0] => 2
[1] => 1
)
)
// identify
$data['key_with_the_most'] = '';
$data['consecutive_pattern'] = 0;
foreach($sequences as $key => $value) {
if(max($value) > $data['consecutive_pattern']) {
$data['consecutive_pattern'] = max($value);
$data['key_with_the_most'] = $key;
}
}
echo '<pre>';
print_r($data);
$data
輸出到:
Array
(
[key_with_the_most] => five
[consecutive_pattern] => 5
)
[你嘗試過什麼?](HTTP:// mattgemmell .com/what-you-you-tried /) – kero
我不知道wh在試圖啊?這就是我問的原因。保持downvoting。 – Sacred
你知道你想要什麼:最長的序列。那麼爲什麼不獲得每個序列,然後比較它們的長度?不知道如何?有創意!相信我,如果你想學習如何解決這個問題,你需要付出努力。如果你想解決它,僱用一個人。另外試着看看我的觀點:對我來說,看起來你在<1min內輸入了這個問題,而沒有其他。如果你不願意這樣做,爲什麼我應該浪費時間? – kero