試試這個:
<?php
$stuff = array(
array('a1', 'a2', 'a3'),
array('b1', 'b2', 'b3'),
array('c1', 'c2', 'c3'),
array('b1', 'b2', 'b3'),
array('d1', 'd2', 'd3'),
array('d1', 'd2', 'd3')
);
$NoteKey = ',';
foreach($stuff as $value) {
if($value[0] === 'b1' || $value[0] === 'd1') {
$NoteKey .=
'<tr><td>' .
$value[0] .
'</td><td>' .
$value[1] .
'</td><td></td></tr>,';
}
}
$NoteKey = trim($NoteKey, ',');
$NoteKey = explode(',', $NoteKey);
$NoteKey = array_unique($NoteKey);
echo '<table>', "\n";
foreach($NoteKey as $value) {
echo "\t", $value, "\n";
}
echo '</table>';
輸出:
<table>
<tr><td>b1</td><td>b2</td><td></td></tr>
<tr><td>d1</td><td>d2</td><td></td></tr>
</table>
這裏的真相是,整個分隔字符串的東西並不是真的需要我在這種情況下,如果在變量中使用逗號,它甚至可能會破壞您的代碼。這裏有一個更好的,少粘性的解決方案:
<?php
$stuff = array(
array('a1', 'a2', 'a3'),
array('b1', 'b2', 'b3'),
array('c1', 'c2', 'c3'),
array('b1', 'b2', 'b3'),
array('d1', 'd2', 'd3'),
array('d1', 'd2', 'd3')
);
$NoteKey = array();
foreach($stuff as $value) {
if($value[0] === 'b1' || $value[0] === 'd1') {
$NoteKey[] =
'<td>' .
$value[0] .
'</td><td>' .
$value[1] .
'</td><td>' .
$value[2] .
'</td>';
}
}
$NoteKey = array_unique($NoteKey);
echo '<table>', "\n";
foreach($NoteKey as $value) {
echo "\t", '<tr>', $value, '</tr>', "\n";
}
echo '</table>';
不要把html標籤數組中,只是把自己的價值觀到陣列中,而當你需要打印出來然後把標籤之間的值。 – Rizier123