好的,我正在想办法做到这一点。我的代码如下:
$context['dp_module_headers'] = array();
然后在函数中,在while循环中,我遍历所有文件夹名并将路径应用于该数组。但是路径是相对于文件夹名称的,所以我需要将文件夹名称包含在已分解数组的值中。
$context['dp_module_headers'] += explode('+', $row[$type . '_header_files']);
返回一个数组,如下所示:
$context['dp_module_headers'][0] = 'source/script.js';
$context['dp_module_headers'][1] = 'source/script.css';
$context['dp_module_headers'][2] = 'source/script.js';
$context['dp_module_headers'][3] = 'mydir/style.css';
我需要在数组中的所有值之前预挂文件夹名称。我只能访问文件夹名称,它是在函数内做爆炸。因此,在另一个文件的另一个函数中,我无法访问这些路径链接到的文件夹名称。
因此,当执行爆炸时,我需要在每个数组的值中的每个路径前附加文件夹名称
所以,基本上,这个数组需要返回以下内容:
$context['dp_module_headers'][0] = 'sitenews/source/script.js';
$context['dp_module_headers'][1] = 'sitenews/source/script.css';
$context['dp_module_headers'][2] = 'userpanel/source/script.js';
$context['dp_module_headers'][3] = 'userpanel/mydir/style.css';
我有一个名为$folder
的变量,每个explode
得到改变,但是我如何将$folder
名称字符串变量添加到每个值的开头,同时爆炸它?还是有更好的方法?
谢谢大家:)
对于PHP 5.3+,您可以这样使用:
$context['dp_module_headers'] += array_map(
function ($path) use ($folder) { return $folder . $path; },
explode('+', $row[$type . '_header_files'])
);
这是有点尴尬与5.2-,在那里你应该保存你的爆炸路径在一个变量中,循环遍历该数组,将$folder
值前置到每个值,然后将结果添加到你的$context
数组。