0

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.

1 Answer 1

2

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) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.