Pass array as reference in PHP 5.2.13?
Question
How can I fix this method to accept reference passing?
It throws Fatal error:
Only variables can be passed by reference
.
/**
* Set a value "deep within" an associative array.
*
* @param array $array_ - Target array to set value on.
* @param mixed $value - A value to set.
* @param string $keyPath - Like 'campaign.pushMessages.sendDate'.
*/
private function setValueForKeyPath(&$array, $value, $keyPath)
{
$keys = explode(".", $keyPath, 2); // Like [ 'campaign', 'pushMessages.sendDate' ]
// If keys is a leaf key...
$isLeaf = (count($keys) == 1);
if ($isLeaf)
{
// ...simply set the value.
$array[$keys[0]] = $value;
}
else
{
// ...or set a sub-array as value.
$this->setValueForKeyPath($array[$keys[0]], $value, $keys[1]);
}
}
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow