found that in PHP you can't reference to reference.
found with this code:
$val = 'OOOOOO';
$b = 'DEFAULT';
$bb = &$b;
$bb = &$val;
echo $b; // prints: DEFAULT
Found solution for me: Approach 3
Approach 3 (pass by reference foreach loop) is faster than Approach 4 (pass by value foreach loop) (same as Gregoire Ducharme answer):
// Approach 3 start
foreach ($arr as $ky => &$vl) {
$arr[$ky] = &$val;
}
// Approach 3 end
// Approach 4 start
foreach ($arr as $ky => $vl) {
$arr[$ky] = &$val;
}
// Approach 4 end
Performance Data (tested on array with long text, 1000000 loops):
Tested 5 times using Approach 3 (By Reference):
Time: 0.32825803756714,
Memory Peak Usage: 596672
Time: 0.33496713638306,
Memory Peak Usage: 596672
Time: 0.33242797851562,
Memory Peak Usage: 596672
Time: 0.35326600074768,
Memory Peak Usage: 596672
Time: 0.33251810073853,
Memory Peak Usage: 596672
Tested 5 times using Approach 4 (By Value):
Time: 0.36558699607849,
Memory Peak Usage: 596672
Time: 0.38936495780945,
Memory Peak Usage: 596672
Time: 0.42887806892395,
Memory Peak Usage: 596672
Time: 0.44330382347107,
Memory Peak Usage: 596672
Time: 0.49368786811829,
Memory Peak Usage: 596672