I have an array with following elements:
$array = ["START","a","a","a","START","b","b","b","START","c","c","c"];
I need to get only the elements of the array AFTER every occurence of the key element "START" into their own array:
Example:
$arr[0] = ["a","a","a"];
$arr[1] = ["b","b","b"];
$arr[2] = ["c","c","c"];
I can then break down each individual array and process how I need.
Thanks!
I’ve played with array slice and such, which seemed to be the closest answer but to no avail.
3
Answers
Design: Collect each element using START as a delimiter.
Implementation:
Note that in PHP, the
[]
syntax is a shorthand way to append an item without needing to specify an index.An approach to this would be to loop through each element and spliting on each encounter of ‘Start’
which would give you a result of
Simple and Performing Solution – O(n)