The function parse_users
returns an array.
I am doing the following in another function:
return reset($this->parse_users($records));
But I get a Strict Standards: Only variables should be passed by reference in...
Is it because I do a reset()
on the function?
Do I have to do it this way:
$users = $this->parse_users($records);
return reset($users);
Or is something else?
That's it exactly. reset
takes a reference to an array as a parameter, so it basically needs a real variable to reference -- even if it is a pass-by-reference value.
why didn't you try your
$users = $this->parse_users($records);
return reset($users);
?
It's correct
The one-line-solution uses an additional pair of brackets; this will turn the reference into a variable and omit the error:
return reset( ( $this->parse_users($records) ) );
From the PHP documentation for reset:
mixed reset ( array &$array )
reset() rewinds array's internal pointer to the first element and returns the value of the first array element.
The PHP reset() function accepts a reference to an array. The strict warning is raised because you're directly passing the result of parse_users to reset, without a way to access that array in your other function.
If you're trying to return the full array (and not just the first value) after it's been reset, you should use:
$users = $this->parse_users($records);
reset($users);
return $users;
Alternatively, if you just want the first value from parse_users, you could just use:
$users = $this->parse_users($records);
return $users[0];
The reset function is only needed when you're iterating over the array and want to make sure you start from the beginning.