我正在创建一个symfony2应用程序,它从文件中搜索和替换字符串
我创建了一个具有以下内容的yaml文件:
parameters:
file.search_and_replace:
"path/dir/filename":
replace: 'word'
with: 'anotherword'
"path_2/dir/filename_2":
replace: 'apples'
with: 'oranges'现在,为了获取内容,我使用了以下语法:
$array = $this->getContainer()->getParameter('file.search_and_replace');如果我像这样转储变量:
var_dump($array);它回来了
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"
}
}我需要找到一种遍历这个数组的方法,这样我就可以将内容传递给我创建的函数,它需要以下参数:
searchAndReplace('filepath','replace_this_word','with_this_word');类似于:
foreach($array as $file)
{
searchAndReplace($file.path,$file.replace,$file.with);
}发布于 2015-01-15 14:55:24
你离我很近。
有两件事:
foreach可以接受key => value语法。用这个你可以得到路径。尝试以下几点:
foreach ($array as $path => $sub) {
searchAndReplace($path,$sub['replace'],$sub['with']);
}https://stackoverflow.com/questions/27966143
复制相似问题