AnEpicPerson
AnEpicPerson

Reputation: 29

PHP adressing the first key in an array that changes

For example, lets say I have an array that looks liked

$stuff = array('random_string_1' => 'random_value_1','random_string_2' => 'random_value_2');

and then I call the asort() function and it changes the array around. How do I get the new first string without knowing what it actually is?

Upvotes: 1

Views: 43

Answers (2)

Georgy Ivanov
Georgy Ivanov

Reputation: 1579

If you need to get the first value, do it like this:

$stuff = array('random_string_1' => 'random_value_1','random_string_2' => 'random_value_2');

$values = array_values($stuff); // this is a consequential array with values in the order of the original array

var_dump($values[0]); // get first value..
var_dump($values[1]); // get second value..

Upvotes: 2

Adikso
Adikso

Reputation: 125

If you want to get the first value of the array you can use reset

reset($stuff);

If you want to also get the key use key

key($stuff);

Upvotes: 3

Related Questions