Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the name of an index in an array [duplicate]

Tags:

php

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?

like image 504
ziiweb Avatar asked Dec 10 '12 09:12

ziiweb


People also ask

How do you find duplicate objects in an array?

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.

Can index have duplicates?

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.

How do I check if an array has duplicates?

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.


2 Answers

Other way is,

$keys = array_keys($array);

echo $keys[0];
like image 102
Dr. Dan Avatar answered Sep 28 '22 16:09

Dr. Dan


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

key($array);
like image 35
Narf Avatar answered Sep 28 '22 16:09

Narf