Ok, so I need to grab the position of 'blah' within this array (position will not always be the same). For example:
$array = ( 'a' => $some_content, 'b' => $more_content, 'c' => array($content), 'blah' => array($stuff), 'd' => $info, 'e' => $more_info, );
So, I would like to be able to return the number of where the 'blah' key is located at within the array. In this scenario, it should return 3. How can I do this quickly? And without affecting the $array array at all.
To find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found.
If you want to access the key of an array in a foreach loop, you use the following syntax: foreach ($array as $key => $value) { ... }
The array_keys() function is used to get all the keys or a subset of the keys of an array. Note: If the optional search_key_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.
The index indicates the position of the element within the array (starting from 1) and is either a number or a field containing a number.
$i = array_search('blah', array_keys($array));
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