0

I have a function asdf() that returns an array ["key" => "value"]. I would like to print out value with one line, but reset() function suggested in similar questions does not work for me because reset only takes variable as a argument and doesent accept function. So if i try reset(asdf()) i get an exception: "Only variables should be passed by reference".

So my question is how can i print "value" from asdf() in a single line only using php native functions.

user1985273
  • 1,817
  • 15
  • 50
  • 85

2 Answers2

1

Actually reset function used to move the array's internal pointer to the first element, I think you must use current function that is return the current element in an array
Try like this

print current(asdf());
Rassoul
  • 174
  • 16
1

Try this:

current(array_values(asdf()));

It uses current to avoid the pass by reference error and array_values to ensure the array passed to current has the first element as it's current element.

Though unless you have a very good reason assigning the array to a variable and then using reset would be better.

dhinchliff
  • 1,106
  • 8
  • 17