我正在使用foreach
循环和If Statement
格式化存储在两个数组中的数据。我对当前的输出感到满意;但是,我有一个问题。我不希望显示没有数据或将- TBD -
作为数据值的类别。
这是我当前的PHP:
<?php
foreach ($finaltitle as $titlenumber => $titlename){
foreach ($techData as $tsnumber => $tsname){
if ($tsnumber == $titlenumber){
$finaltsdata = "<b>".$titlename." (".$tsnumber.") "."</b>: ".$tsname."<br>";
echo $finaltsdata;
}
}
}
?>
及其相应的输出:
Vehicle Name (1) : Audi S4
Body Style (2) : 5 Door Wagon
Drivetrain (6) : All-Wheel Drive
EPA Classification (7) : Small Station Wagon
Passenger Capacity (8) : 5
Passenger Volume (9) : 90.8
Base Curb Weight (10) : 4101
EPA Fuel Economy Est - City (26) : 14
EPA Fuel Economy Est - Hwy (27) : 21
Dead Weight Hitch - Max Trailer Wt. (31) : - TBD -
Dead Weight Hitch - Max Tongue Wt. (32) : - TBD -
Wt Distributing Hitch - Max Trailer Wt. (33) : - TBD -
Wt Distributing Hitch - Max Tongue Wt. (34) : - TBD -
Engine Order Code (40) :
Engine Type (41) : Gas V8
Displacement (42) : 4.2L/254
Fuel System (43) : SEFI
SAE Net Horsepower @ RPM (48) : 340 @ 6800
SAE Net Torque @ RPM (49) : 302 @ 3500
Trans Order Code (51) :
Trans Type (52) : 6
因此对于此输出,我不希望显示以下类别,因为它们没有数据值或数据值等于- TBD -
。
Dead Weight Hitch - Max Trailer Wt. (31) : - TBD -
Dead Weight Hitch - Max Tongue Wt. (32) : - TBD -
Wt Distributing Hitch - Max Trailer Wt. (33) : - TBD -
Wt Distributing Hitch - Max Tongue Wt. (34) : - TBD -
Engine Order Code (40) :
Trans Order Code (51) :
非常感谢您的帮助!
答案 0 :(得分:2)
通过Continue语句跳过不需要的项目
if ($tsnumber == $titlenumber){
if(empty($tsname) or $tsname === '- TBD -') continue;
$finaltsdata = "<b>".$titlename." (".$tsnumber.") "."</b>: ".$tsname."<br>";
echo $finaltsdata;
}