PHP: Find An Array Sequence In An Array
I was looking for a function that searched an array for another array, and after not finding one I decided to write it. What I was looking for was a function that took a smaller array and searched for that exact sequence of items in a larger array. As it happens, PHP does have a number of array search functions, but they didn't produce the correct result.
The array_search() function does accept an array as the needle to be searched for, but this does a multi-dimensional search instead. I also saw some techniques using array_intersect() or array_diff(), and although these functions were able to find one array inside another I was interested in the sequence.
The implementation of the function contains a two loops, one for going through the haystack array and one for the needle array. If the number of matches found equals the number of items in the needle array then the function returns true.
Here is the sequence_in_array() function.