我有這樣的代碼,以從文件創建我的數組:過濾陣列結果在每個循環
<?php
$servers = array();
$handle = @fopen("data/data.txt", "r");
if ($handle) {
while (($buffer = fgets($handle)) !== false) {
$line = explode("|", $buffer);
$servers[] = array(
"name" => $line[0],
"ip" => $line[1],
"type" => $line[2],
);
}
fclose($handle);
}
?>
然後我有這樣的代碼來顯示陣列:
<?php
foreach ($servers as $name => $servers): ?>
<td style="width:340px;"> <?php echo $servers['name']; ?></td>
<td style="width:240px;"><?php echo $servers['ip']; ?></td>
</tr>
<?php endforeach; ?>
這是陣列樣品:
Array(
[0] => Array
(
[name] => aaa
[ip] => 123
[type] => good
)
[1] => Array
(
[name] => bbb
[ip] => 345
[type] => good
)
)
假設我需要過濾的結果與陣列型好, 即時通訊,試圖這個代碼,但它只返回最後一個數組:
<?php
foreach ($servers as $name => $servers): ?>
<?php if($servers['type']=="good"){?>
<td style="width:340px;"> <?php echo $servers['name']; ?></td>
<td style="width:240px;"><?php echo $servers['ip']; ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
我嘗試過,但沒有什麼結果 – Ryewell
改變,你會發布完整的HTML表,我想你錯過了一個標籤'tr'或'td' –
我也嘗試沒有HTML tags..still相同的輸出 – Ryewell