基本上,我有多个包含大量PHP变量的文件,我正在尝试创建一种方法来将它们读入每个文件的子数组中,每个变量都在数组中。
<?php
$data = Array();
$getData = glob('siteData/*.vars.php', GLOB_BRACE);
foreach($getData as $tempData) {
$dataKey = str_replace(".vars.php", "", str_replace("siteData/", "", $tempData));
$data[$dataKey] = Array();
//foreach varaible in file
//$data[$dataKey][$varaibleName] = $variable
}
?>
评论出来的是我需要帮助的位,因为我不知道该怎么做。 请帮忙,TIA。
答案 0 :(得分:1)
经过一些帮助后,回答代码如下:
<?php
$data = Array();
$getData = glob('siteData/*.vars.php', GLOB_BRACE);
foreach($getData as $tempData) {
$dataKey = str_replace(".vars.php", "", str_replace("siteData/", "", $tempData));
$data[$dataKey] = Array();
$dataFile = file_get_contents($tempData);
preg_match_all('/\$[A-Za-z0-9-_]+\s?=\s?["\'].+["\'];/', $dataFile, $dataVars);
$dataVars = $dataVars[0];
foreach($dataVars as $variable) {
$variable = explode("=", $variable);
$varaibleName = ltrim(trim($variable[0]), "$");
$variable = trim(trim(rtrim(trim($variable[1]), ";"), "'"), '"');
$data[$dataKey][$varaibleName] = $variable;
}
}
print_r($data);
?>