php get first array key

Get first key in a (possibly) associative array?

What’s the best way to determine the first key in a possibly associative array? My first thought it to just foreach the array and then immediately breaking it, like this:

23 Answers 23

2019 Update

Starting from PHP 7.3, there is a new built in function called array_key_first() which will retrieve the first key from the given array without resetting the internal pointer. Check out the documentation for more info.

It’s essentially the same as your initial code, but with a little less overhead, and it’s more obvious what is happening.

If you wanted the key to get the first value, reset actually returns it:

There is one special case to watch out for though (so check the length of the array first):

php get first array key. Смотреть фото php get first array key. Смотреть картинку php get first array key. Картинка про php get first array key. Фото php get first array key

Interestingly enough, the foreach loop is actually the most efficient way of doing this.

Since the OP specifically asked about efficiency, it should be pointed out that all the current answers are in fact much less efficient than a foreach.

I did a benchmark on this with php 5.4, and the reset/key pointer method (accepted answer) seems to be about 7 times slower than a foreach. Other approaches manipulating the entire array (array_keys, array_flip) are obviously even slower than that and become much worse when working with a large array.

Foreach is not inefficient at all, feel free to use it!

Edit 2015-03-03:

Benchmark scripts have been requested, I don’t have the original ones but made some new tests instead. This time I found the foreach only about twice as fast as reset/key. I used a 100-key array and ran each method a million times to get some noticeable difference, here’s code of the simple benchmark:

Источник

How to get the first item from an associative PHP array?

If I had an array like:

And I wanted to get the first item out of that array without knowing the key for it, how would I do that? Is there a function for this?

16 Answers 16

reset() gives you the first value of the array if you have an element inside the array:

It also gives you FALSE in case the array is empty.

php get first array key. Смотреть фото php get first array key. Смотреть картинку php get first array key. Картинка про php get first array key. Фото php get first array key

php get first array key. Смотреть фото php get first array key. Смотреть картинку php get first array key. Картинка про php get first array key. Фото php get first array key

PHP reset and so has side effects (it resets the internal pointer of the array), so you might prefer using array_slice to quickly access a copy of the first element of the array:

Assuming you want to get both the key and the value separately, you need to add the fourth parameter to array_slice :

To get the first item as a pair ( key => value ):

Simple modification to get the last item, key and value separately:

performance

If the array is not really big, you don’t actually need array_slice and can rather get a copy of the whole keys array, then get the first item:

A notable exception is when you have the first key which points to a very large and convoluted object. In that case array_slice will duplicate that first large object, while array_keys will only grab the keys.

PHP 7.3+

You still had better check that the array is not empty though, or you will get an error:

Источник

PHP array_keys

Summary: in this tutorial, you will learn how to use the PHP array_keys() function to get the keys of an array.

Introduction to the PHP array_keys function

The PHP array_keys() function accepts an array and returns all the keys or a subset of the keys of the array.

The array_keys() function returns an array that contains all the keys in the input array.

PHP array_keys() function examples

Let’s take some examples of using the array_keys() function.

1) Using the array_keys() function example

The following example shows how to get all keys of an indexed array:

The following example uses the array_keys() function to get the keys of the array whole value is 20:

The array_keys() function returns the key 1 because key 1 contains the value 20.

2) Using PHP array_keys() function with an associative array example

The following example illustrates how to use the array_keys() function with an associative array:

The following example uses the array_keys() function to get the keys whose values equal 1:

To enable the strict equality comparison (===) when searching, you pass true as the third argument of the array_keys() function like this:

Now, the array_keys() function returns an empty array.

Finding array keys that pass a test

The following function returns the keys of an array, which pass a test specified a callback:

The following example uses the array_keys_by() function above to find the keys that contain the string ‘_post’ :

Источник

get first and last element in array

hey there i have this array:

how to get the first and the last element out from this array, so i will have:

anybody knows how to get those elements from the array?

i already tried this:

6 Answers 6

reset() and end() does exactly this.

reset() : Returns the value of the first array element, or FALSE if the array is empty.

end() : Returns the value of the last element or FALSE for empty array.

NOTE: This will reset your array pointer meaning if you use current() to get the current element or you’ve seeked into the middle of the array, reset() and end() will reset the array pointer (to the beginning and to the end):

As of PHP 7.3, array_key_first and array_key_last is available

You can use reset() to get the first:

reset() rewinds array’s internal pointer to the first element and returns the value of the first array element.

And end() to get the last:

end() advances array’s internal pointer to the last element, and returns its value.

php get first array key. Смотреть фото php get first array key. Смотреть картинку php get first array key. Картинка про php get first array key. Фото php get first array key

For first element: current($arrayname);

For last element: end($arrayname);

current(): The current() function returns the value of the current element in an array. Every array has an internal pointer to its «current» element, which is initialized to the first element inserted into the array.

Источник

How to get array key from corresponding array value?

php get first array key. Смотреть фото php get first array key. Смотреть картинку php get first array key. Картинка про php get first array key. Фото php get first array key

5 Answers 5

You could use array_search() to find the first matching key.

php get first array key. Смотреть фото php get first array key. Смотреть картинку php get first array key. Картинка про php get first array key. Фото php get first array key

You can use the array_keys function for that.

Example:

This will get the key from the array for value blue

php get first array key. Смотреть фото php get first array key. Смотреть картинку php get first array key. Картинка про php get first array key. Фото php get first array key

php get first array key. Смотреть фото php get first array key. Смотреть картинку php get first array key. Картинка про php get first array key. Фото php get first array key

If you do this often, create a reverse array/hash that maps values back to keys. Keep in mind multiple keys may map to a single value.

Your array values can be duplicates so it wont give you exact keys. However the way i think is fine is like iterate over and read the keys

Not the answer you’re looking for? Browse other questions tagged php arrays or ask your own question.

Linked

Related

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.17.40238

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *