我有一份信件清单。我将这个字母列表与字母表数组进行比较,得到差异。然后将它们放在一个数组中,这样我就可以将它输出为一个列表。
//start the array
$alphabet = array();
//the letters I'm using
$letters = array('A','B','C','D','E','F','G','H','I','J','L','M','N','O','P','R','S','T','V');
//place into true array
foreach($letters as $l)
$alphabet['true'][] = $l;
//alphabet array, place into false array
foreach (range('A','Z') as $char)
$alphabet['false'][] = $char;
//the not used array by getting the difference of true and false arrays
$alphabet['actual'] = array_diff($alphabet['false'], $alphabet['true']);
//merge the arrays into one array
$new = array_merge($alphabet['true'],$alphabet['actual']);
//sort them naturally
natsort($new);
//list the results
echo "All together now: <pre>"; print_r($new); echo "</pre>";
在将它们放入大数组之前,有没有办法对每个不同的数组键值进行样式设置?沿着未使用的字母的线条是不同的颜色?或者我是以错误的方式来做这件事的?感谢您的任何见解。
答案 0 :(得分:2)
如果我在哪里,我会做这样的事情。
//start the array
$alphabet = array();
//the letters I'm using
$used = array('A','B','C','D','E','F','G','H','I','J','L','M','N','O','P','R','S','T','V');
$alphabet = range('A','Z');
echo "<ul>";
foreach($alphabet as $letter){
if (in_array($letter, $used)){
echo "<li class='used'>".$letter."</li>";
} else {
echo "<li class='unused'>".$letter."</li>";
}
}
echo "</ul>";
制作一些css规则
li.used { color:green; }
li.unused { color:red; }