PHP: Get first and last element entries in array and it's keys
I am currently dealing with some task, that requires to get keys of array at which element appears for the first and very last time.
Example:
$array = [1,2,3,2,4,1,1,2,3];
getFor(1); // keys 0 and 6
getFor(2); // keys 1 and 7
getFor(3); // keys 2 and 8
I would appreciate any help with this one.
P.S. Simple foreaching the array is not effective here, 'cause it takes too much time for very long arrays (over 1000000 entries). So I'm looking for not that obvious solution.
Also,getFor
return type is not essential, it could be either a string or array of two values.
Answer
Solution:
Answer
Solution:
Heres an OOP solution: