我有一个属性表,它有六列.用户上传照片和图像名称存储在列中.
现在我想计算每行为空的列数.
我已经能够做到这一点,但代码看起来太长了,我想编写高效的代码,是否有办法有效地重写以下内容.
while($data=$select->fetch()){
$imagecounter=0;
if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "" && $data['property_image4'] !== "" && $data['property_image5'] !== "" && $data['property_image6'] !== "") {
echo $imagecounter=6;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "" && $data['property_image4'] !== "" && $data['property_image5'] !== "") {
echo $imagecounter=5;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "" && $data['property_image4'] !== "") {
echo $imagecounter=4;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "") {
echo $imagecounter=3;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "") {
echo $imagecounter=2;
} else if ($data['property_image1'] !== "") {
echo $imagecounter=1;
}
}
解决方法:
你可以这样做: –
while($data=$select->fetch()){
$data1 = array($data['property_image1'],$data['property_image2'],$data['property_image3'],$data['property_image4'],$data['property_image5'],$data['property_image6']);
$count = count($data1); // count of original array
$count1 = count(array_filter($data1)); // remove empty indexes and count the values
echo "empty columns number is :-".($count-$count1);
}
注意: – $count1是非空值的计数