1

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?

2

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

Comments

2

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..

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.