Let's say we have this array:
Array ( [0] => 10 [1] => 45 [2] => 23 )
How can I determine the position of element '45' in this array?
I'm using PHP.
Thank you.
In order to find the index of an element Stream package provides utility, IntStream. Using the length of an array we can get an IntStream of array indices from 0 to n-1, where n is the length of an array.
In MATLAB the array indexing starts from 1. To find the index of the element in the array, you can use the find() function. Using the find() function you can find the indices and the element from the array. The find() function returns a vector containing the data.
An array index is an integer indicating a position in an array. Like Strings, arrays use zero-based indexing, that is, array indexes start with 0. The following displays the indexes and values in an array with 10 elements of type int. index.
Use list. index() to find the position of an element in a list. Call list. index(value) to return the position of value in list .
Use array_search
to get the key to a value:
$key = array_search(45, $arr);
And if you want to get its position in the array, you can search for the index of the key in the array of keys:
$offset = array_search($key, array_keys($arr));
So with an array like the following you will still get 1
as result:
$arr = array('foo' => 10, 'bar' => 45, 'baz' => 23);
Google to the rescue: array_search
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With