Basically, I have multiple files containing loads of PHP variables, and i'm trying to create a way to read them in to a subarray for each file with each variable being in the array.
<?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
}
?>
The bit commented out is the bit I need help with as I have no idea how to do it. Please help, TIA.
After a bit of help, the answering code is as follows:
<?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);
?>