如何无需额外变量解决PHP中“Only variables should be passed by reference”错误
Absolutely, you can solve this without cluttering your code with tons of temporary variables! Let's start with why you're seeing that error, then dive into clean solutions.
Why the Error Happens
array_shift() requires a variable passed by reference because it modifies the array it receives (it removes the first element and adjusts the array's internal pointer). When you pass the result of array_column() directly to array_shift(), you're passing a temporary value (not a stored variable), which PHP won't allow to be modified by reference. That's exactly what triggers the error.
Solution 1: Directly Access the First Element (Recommended)
Since your goal is to get the first value from the array_column() result, you don't even need array_shift() here. Just access the index [0] directly:
$video["upload"]["url"] = array_column($json, 'url')[0] ?? null;
The ?? null handles cases where array_column() returns an empty array (so you don't get an "Undefined offset" warning). If you're certain the array will always have at least one element, you can omit the null coalescing operator.
Solution 2: Use an Inline Assignment for Reference
If you specifically need to use array_shift() (for example, if you need to modify the array later), you can wrap an assignment in parentheses to pass a variable reference in one line:
$video["upload"]["url"] = array_shift(($temp = array_column($json, 'url')));
The parentheses make the assignment evaluate to the variable $temp, which can be passed by reference to array_shift(). Note that $temp will exist in the current scope after this line, but it's a one-off variable that doesn't require a separate line of code.
Either approach lets you avoid creating separate variable declarations for every instance of this pattern.
内容的提问来源于stack exchange,提问作者Bruno Andrade

