问题
I have the problem that I get the error
PHP Fatal error: Call-time pass-by-reference has been removed in....
I discovered some similar questions e.g.
- PHP 5.4 Call-time pass-by-reference - Easy fix available?
- Call-time pass-by-reference warning
But nothing offers a reals answers how situations be can solved where you NEED to declare the passed value as refference by runtime for e.g. buildin function which you cant change function declaration ??.
e.g. for this example third array_walk parameter as reference: ?
I tried to use this solution to change the indexes of my array with this code:
function __reindex(&$v,$k, &$aReindexed)
{
$kNew = $k+100;
$aReindexed[$kNew] = $v;
}
$aTest = array(4,"f","_","test");
array_walk($aTest,"__reindex", &$aReindexed );
The Code without the refference is not working (the new array is not changed and stays empty).
And the Code with the refference it works, but not in php 5.4 and higher.
So whats the way so handle such situations ?
p.s. if anybody likes to say "declare the $k variable in your __reindex function as refference" then that wont work (that was the first way I tried)
回答1:
I found a way what maybe helps some people.
Try this Code
$aTest = array(4,"f","_","test");
$aReindexed = array();
array_walk($aTest, function(&$v,$k) use (&$aReindexed) {
$kNew = $k+100;
$aReindexed[$kNew] = $v;
} );
print_r($aReindexed);
This coud would work in php 5.4 and above.
But it works not for lower php versions, and you can not use it with an already existing (non-anonym) callback function, because the USE-Keyword only works when you create a new Closure function.
来源:https://stackoverflow.com/questions/59213965/php-5-4-call-time-pass-by-reference-how-to-fix-it