I have an array of values, all strings. There are also 2 “marker” entries in the array, that signify what the values that follow are. Basically it’s set up like so:
$array['1','2','3','A','5','6','7','B','8','9']
I need to retrieve the array indices after A, but before B. So, in this example, the indices of 5, 6, and 7.
I was thinking about doing this with a forloop and some iterative if statements.. But I can’t seem to figure it out. Any help is much appreciated 🙂
EDIT: also worth noting that A and B are not always at the same position in the array; some users will have more data between A and B than others, some won’t have A and B at all. But if A is present, I need to get the values following it. If A and B are present they will also always have the same value (A and B).
EDIT2: This is what I’m currently trying as per PeeHaa’s suggestions. A is “registered:current” and B is “registered:next”. In his testing he is getting the value that comes after A but before B, however in my tests I am only getting the value of A (“registered:current”). It just outputs “registered:current” instead of the value that exists between the 2 markers.
function getValuesBetweenMarkers($theArray, $startMarker = 'registered:current', $endMarker = 'registered:next')
{
$offset = array_search($startMarker, $theArray) + 1;
$length = array_search($endMarker, $theArray) - $offset;
return array_slice($theArray, $offset, $length, true);
}
I think this should work: