PHP用for循环遍历数组,如何能把数组中所有的2变成红色?代码如下: $a = array(1, 20
PHP用for循环遍历数组,如何能把数组中所有的2变成红色?代码如下:$a=array(1,2,3,2,4,5,2,3,2,3,4,1,2,3,4,2,3,4,8,2,3...
PHP用for循环遍历数组,如何能把数组中所有的2变成红色?代码如下:
$a = array(1,2,3,2,4,5,2,3,2,3,4,1,2,3,4,2,3,4,8,2,3,8,7,4,6,1,2,4,1,2,3,4,8,7,1,6,2,3,9,4,8,1,2,3,4,9,1,8,2,3,4,9,8,1,2,6,3,4,1,2,3,4,1);
$c=count($a);
for($i=0;$i<$c;$i++){
echo $a[$i];
} 展开
$a = array(1,2,3,2,4,5,2,3,2,3,4,1,2,3,4,2,3,4,8,2,3,8,7,4,6,1,2,4,1,2,3,4,8,7,1,6,2,3,9,4,8,1,2,3,4,9,1,8,2,3,4,9,8,1,2,6,3,4,1,2,3,4,1);
$c=count($a);
for($i=0;$i<$c;$i++){
echo $a[$i];
} 展开
2个回答
展开全部
<?php
$a = array(1,2,3,2,4,5,2,3,2,3,4,1,2,3,4,2,3,4,8,2,3,8,7,4,6,1,2,4,1,2,3,4,8,7,1,6,2,3,9,4,8,1,2,3,4,9,1,8,2,3,4,9,8,1,2,6,3,4,1,2,3,4,1);
for($i=0;$i<count($a);$i++){
if($a[$i]==2){
echo "<span style='color:red'>".$a[$i].",</span>";
}else{
echo "<span>".$a[$i].",</span>";
}
}
?>
给他加一个标签,然后设置color颜色就可以了,结果如下:
操作数据推荐用:foreach();
<?php
$a = array(1,2,3,2,4,5,2,3,2,3,4,1,2,3,4,2,3,4,8,2,3,8,7,4,6,1,2,4,1,2,3,4,8,7,1,6,2,3,9,4,8,1,2,3,4,9,1,8,2,3,4,9,8,1,2,6,3,4,1,2,3,4,1);
foreach($a as $k){
if($k==2){
echo "<span style='color:red'>".$k.",</span>";
}else{
echo "<span>".$k.",</span>";
}
}
?>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询