我每月从用户数据的日志文件中提取数据。代码本身有效,但似乎不优雅。我正在寻找有关如何消除它的想法。感谢任何帮助。
我有以下代码。 data_manip函数在get_files中调用,它将每个月的相关数据分配到一个名为file_resource的数组中。然后调用函数data_manip,它使用' extract'根据get_files函数中月份名称定义的键名来定义带有$ names的数组。然后,我根据放在数据文件夹中的文件(我没有包含的代码)确定该变量是否存在。 data_analytic函数分析数据并将结果输出到html页面。目前这个解决方案似乎很笨拙。
我想要做的是确定每个月名称变量是否存在(1月 - 12月),然后如果每次查询验证为真,则爆炸与该特定月份名称对应的数组$ name的结果并将结果分配给一个新的数据数组,称为$ january_data,或$ february_data等。
function get_files($month_names){
foreach ($month_names as $month_name) {
$file_resource[] = array(
$month_name =>
file("data/$month_name.log")
);
}
data_manip($file_resource);
return $file_resource;
}
function data_manip($file_resource){
foreach ($file_resource as $month_data) {
extract($month_data);
}
if (isset($january)){
foreach ($january as $january_values) {
$january_data[] = explode(' ', $january_values);
}
echo "<h2>January</h2>";
data_analytic($january_data);
}
if (isset($february)){
foreach ($february as $february_values) {
$february_data[] = explode(' ', $february_values);
}
echo "<h2>February</h2>";
data_analytic($february_data);
}
if (isset($march)){
foreach ($march as $march_values) {
$march_data[] = explode(' ', $march_values);
}
echo "<h2>March</h2>";
data_analytic($march_data);
}
if (isset($april)){
foreach ($april as $april_values) {
$april_data[] = explode(' ', $april_values);
}
echo "<h2>April</h2>";
data_analytic($april_data);
}
if (isset($may)){
foreach ($may as $may_values) {
$may_data[] = explode(' ', $may_values);
}
echo "<h2>May</h2>";
data_analytic($may_data);
}
if (isset($june)){
foreach ($june as $june_values) {
$june_data[] = explode(' ', $june_values);
}
echo "<h2>June</h2>";
data_analytic($june_data);
}
if (isset($july)){
foreach ($july as $july_values) {
$july_data[] = explode(' ', $july_values);
}
echo "<h2>July</h2>";
data_analytic($july_data);
}
if (isset($august)){
foreach ($august as $august_values) {
$august_data[] = explode(' ', $august_values);
}
echo "<h2>August</h2>";
data_analytic($august_data);
}
if (isset($september)){
foreach ($september as $september_values) {
$september_data[] = explode(' ', $september_values);
}
echo "<h2>September</h2>";
data_analytic($september_data);
}
if (isset($october)){
foreach ($october as $october_values) {
$october_data[] = explode(' ', $october_values);
}
echo "<h2>October</h2>";
data_analytic($october_data);
}
if (isset($november)){
foreach ($november as $november_values) {
$november_data[] = explode(' ', $november_values);
}
echo "<h2>November</h2>";
data_analytic($november_data);
}
if (isset($december)){
foreach ($december as $december_values) {
$december_data[] = explode(' ', $december_values);
}
echo "<h2>December</h2>";
data_analytic($december_data);
}
}