1

I'm creating a symfony2 application that searches and replaces strings from files

I created a yaml file with the following content:

parameters:
         file.search_and_replace:
                 "path/dir/filename":
                        replace: 'word'
                        with: 'anotherword'

                 "path_2/dir/filename_2":
                        replace: 'apples'
                        with: 'oranges'

now to fetch the content I use the following syntax:

$array = $this->getContainer()->getParameter('file.search_and_replace');

If I dump the variable like this:

var_dump($array);

it returns

array(2) 
{
  'path/file/filename' =>
    array(2) 
    {
      'replace' => string(4) "word"
      'with' => string(11) "anotherword"
    }
  'path_2/file/filename_2' =>
    array(2) 
    {
     'replace' => string(6) "apples"
     'with' =>    string(7) "oranges"
    }
}

I need to find a way to loop through this array so I can pass the content to a function I created, that needs the following parameters:

searchAndReplace('filepath','replace_this_word','with_this_word');

something like:

foreach($array as $file)
{
    searchAndReplace($file.path,$file.replace,$file.with);  
}

1 Answer 1

2

You're close.

Two things:

  1. foreach can accept a key => value syntax. Using this you can get the path.
  2. PHP does not use dot notation for arrays. It uses brackets.

Try the following:

foreach ($array as $path => $sub) {
    searchAndReplace($path,$sub['replace'],$sub['with']);  
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.