What\'s the best way to search for consecutive values in an array?
For example, searching for array(\'a\', \'b\')
in array(\'x\', \'a\', \'b\', \'c\
Haven't tested this, but something like this should do:
function consecutive_values(array $needle, array $haystack) {
$i_max = count($haystack)-count($needle);
$j_max = count($needle);
for($i=0; $i<$i_max; ++$i) {
$match = true;
for($j=0; $j<$j_max; ++$j) {
if($needle[$j]!=$haystack[$i+$j]) {
$match = false;
break;
}
}
if($match) {
return $i;
}
}
return -1;
}