7

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?

0

2 Answers 2

12

Other way is,

$keys = array_keys($array);

echo $keys[0];
Sign up to request clarification or add additional context in comments.

1 Comment

maybe shorten it $firstKey = array_keys($array)[0]; ?
11

Assuming that you haven't used each(), next(), prev() or in any other manner have you advanced the array pointer:

key($array);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.