I have an array:
$list = array('string1', 'string2', 'string3');
I want to get the index for a given value (i.e. 1 for string2 and 2 for string3)
All I want is the position of the strings in the array
- string1 is 0
- string2 is 1
- string3 is 2
How to achieve this?
array_searchis the way to do it.From the docs:
You could loop over the array manually and find the index but why do it when there’s a function for that. This function always returns a key and it will work well with associative and normal arrays.