Possible Duplicate:
Get first key in a [possibly] associative array?
I have this array:
Array
(
['foobar'] => Array
(
[key1] => value1
[key2] => value2
)
)
I want to get to name of the first index (foobar). How can I do it?
Using the indexOf() method In this method, what we do is that we compare the index of all the items of an array with the index of the first time that number occurs. If they don't match, that implies that the element is a duplicate. All such elements are returned in a separate array using the filter() method.
Duplicate indexes are those that exactly match the Key and Included columns. That's easy. Possible duplicate indexes are those that very closely match Key/Included columns.
To check if an array contains duplicates: Use the Array. some() method to iterate over the array. Check if the index of the first occurrence of the current value is NOT equal to the index of its last occurrence. If the condition is met, then the array contains duplicates.
Other way is,
$keys = array_keys($array);
echo $keys[0];
Assuming that you haven't used each()
, next()
, prev()
or in any other manner have you advanced the array pointer:
key($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