1

这是csv格式:

ServerA,OK,app1
ServerB,OK,app2
ServerC,OK,app3
ServerD,,app4
ServerA,OK,app2
ServerB,,app3
ServerC,OK,app1
ServerD,OK,app2
ServerA,OK,app3
ServerB,OK,app1
ServerC,,app2

我想要显示的是:

Server:     App1:     App2:  App3:  App4
ServerA      OK        OK     OK     OK
SERverB      OK        OK     N/A    OK

如果第二个字段为空,则显示 N/A。

这是我尝试过的:

$file = "serverdata.txt";
$fp = fopen($file, "r") or die("Can't open file serverdata.txt");


while(!feof($fp)) {
    $data = fgets($fp, 1024);
    $navData = explode(',',$data);

            $host = $navData[0];
            $app = $navData[2];
            $status = $navData[1];
            $myData[$host][$app] = $status;


}


foreach ($myData as $tempHost) {
if (isset($myData[$tempHost]['app1'])) {
          $App1_status =  $mydata[$tempHost]['app1'];
    }
    else {
           $App1_status = "N/A";
    }

.
.
.
.
<same for all other apps status >

print "<TD>$tempHost</TD><TD>$App1_status</TD><TD>$App2_status</TD><TD>$App3_status</TD>  <TD>$App4_status</TD></TR>\n";
}


}

似乎没有得到它的工作,任何帮助将不胜感激。

4

4 回答 4

3

您的应用程序中基本上有两个部分。第一部分是读入文件并将数据存储在内存中。你实际上做得很好我主要在我的例子中只使用了不同的变量名,我正在使用它,SplFileObject因为它已经为我们解析了逗号分隔的值:

$file = new SplFileObject('serverdata.txt');
$file->setFlags(SplFileObject::READ_CSV);
$hosts = array();
$apps = array();
foreach($file as $line) {
    list($host, $status, $app) = $line;
    $apps[$app] = 1;
    $hosts[$host][$app] = $status;
}

每一行都被解析为$host,$status$app。我像您一样保存主要数据(此处保存到$hosts)。我还将应用程序名称保存到另一个名为的数组$apps中,以便以后可以基于主机应用程序构建矩阵。我将应用程序名称保存为键,这是一种删除重复项的简单方法,因为在数组中每个键只能存在一次。

第二部分是输出数据。我在这里以纯文本形式进行,如果您需要使用 HTML,它可能更容易:

echo "+----------+", str_repeat('------+', count($apps)), "\n";
echo "| Server   |";
foreach($apps as $name => $v) {
    echo " $name |";
}
echo "\n", "+----------+", str_repeat('------+', count($apps)), "\n";

foreach($hosts as $host => $hostApps) {
    printf("| %' -8s |", $host);
    foreach($apps as $name => $v) {
        $value = isset($hostApps[$name]) ? $hostApps[$name] : "N/A";
        printf("  %' -3s |", $value);
    }
    echo "\n", "+----------+", str_repeat('------+', count($apps)), "\n";
}

正如你所看到的,另一个foreach里面有一个。一个适用于所有主机,另一个适用于所有应用程序。

然后,这将为您提供的数据创建输出serverdata.txt

+----------+------+------+------+------+
| Server   | app1 | app2 | app3 | app4 |
+----------+------+------+------+------+
| ServerA  |  OK  |  OK  |  OK  |  N/A |
+----------+------+------+------+------+
| ServerB  |  OK  |  OK  |      |  N/A |
+----------+------+------+------+------+
| ServerC  |  OK  |      |  OK  |  N/A |
+----------+------+------+------+------+
| ServerD  |  N/A |  OK  |  N/A |      |
+----------+------+------+------+------+

我希望这个例子是有帮助的。您还可以在输出数据之前执行第三步,以规范化数组,以便每个主机(行)已经拥有所有应用程序的数据。然后做输出就更容易了。但我把它留作练习。

于 2012-08-13T23:09:06.820 回答
1
$filename = 'serverdata.txt';
$data_lines = file($filename);
$server_app_data = array();
$servers = array();
$apps = array();
foreach($data_lines as $line) {
  list($server,$status,$app) = explode(',',$line);
  $server_app_data[$server][$app] = $status;
  if(!in_array($server, $servers)) $servers[] = $server;
  if(!in_array($app, $apps)) $apps[] = $app;
}

$table_content = '<table><thead><tr><th>Server:</th>';
foreach($apps as $app) $table_content .= '<th>'.ucwords(trim($app)).':</th>';
$table_content .= '</tr></thead><tbody>';
foreach($servers as $server) {
  $table_content .= '<tr><th>'.$server.'</th>';
  foreach($apps as $app) {
    $table_content .= '<td>'.($server_app_data[$server][$app]?:'N/A').'</td>';
  }
  $table_content .= '</tr>';
}
$table_content .= '</table>';

echo $table_content;

生成下表:

Server:     App1:   App2:   App3:   App4:
ServerA     OK      OK      OK      N/A
ServerB     OK      OK      N/A     N/A
ServerC     OK      N/A     OK      N/A
ServerD     N/A     OK      N/A     N/A
于 2012-08-13T23:06:24.127 回答
0

该行将foreach ($myData as $tempHost)$tempHost. 你想要的是(然后你可以用foreach ($myData as $tempHost=>$hostData)替换所有实例)。$myData[$tempHost]$hostData

于 2012-08-13T22:47:11.880 回答
0
foreach ($myData as $host=>$apps) {
print "<TD>$host</TD>";
foreach($apps as $status)
    print "<TD>".($status!=""?$status:"N/A")."</TD>";
}
print "\n";
于 2012-08-13T23:07:51.007 回答