How to get the first key in a PHP associative array (without resetting the pointer)?
Hello everyone! I'm a beginner PHP developer and I'm having an issue with arrays.
I have an associative array that can be either empty or contain data. I need to get only the first key of this array without modifying its internal pointer. The problem is that I don't know in advance what keys the array will have — they can be either numeric or string.
Here's an example of my code:
$users = [
'john' => ['name' => 'John', 'age' => 30],
'jane' => ['name' => 'Jane', 'age' => 25],
'bob' => ['name' => 'Bob', 'age' => 35]
];
// I need to get the first key 'john'
$firstKey = ???
I've tried using the key(), reset(), and array_keys() functions, but
No answers yet.
Log in to leave an answer
Blogs