Reputation: 10521
This script check every line of some text for "FIRST" and "LAST" words, and trying to replace them by entries in $temp_names
array.
$temp_names = array('FIRST' => array('John','Jack'),'LAST' => array('Doe','Smith'));
for ($i=0; $i < count($lines); $i++)
{
$lines[$i] = preg_replace_callback("/FIRST|LAST/",
function($matches) use ($temp_names){
return array_shift($temp_names[$matches[0]]); }, $lines[$i]);
}
i have problem with return array_shift()
in closure function. It correctly returns the first entry, but the entry stays in array. So every time it return "John" and "Doe". Why is that?
thanks.
Upvotes: 0
Views: 664
Reputation: 51950
In order to have any changes to the $temp_names
array (such as shifting a value), you need to use
it by reference like
function ($matches) use (&$temp_names)
Upvotes: 2