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?

  • The answer to this will help: http://stackoverflow.com/questions/1921421/get-the-first-element-of-an-array – Ben Plummer Dec 23 '16 at 22:29
  • This, `echo $stuff[key($stuff)];`. Here's the doc, [http://php.net/manual/en/function.key.php](http://php.net/manual/en/function.key.php) – Rajdeep Paul Dec 23 '16 at 22:32

2 Answers2

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);
Adikso
  • 124
  • 2
  • 4
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..
Georgy Ivanov
  • 1,573
  • 1
  • 17
  • 24