0

I am formatting data that is stored in two arrays using foreach loops and an If Statement. I am happy with my current output; however, I am having one issue. I don't want categories with No data or - TBD - as a data value to be displayed.

Here is my current PHP:

<?php
 foreach ($finaltitle as $titlenumber => $titlename){
   foreach ($techData as $tsnumber => $tsname){
     if ($tsnumber == $titlenumber){
       $finaltsdata = "<b>".$titlename." (".$tsnumber.") "."</b>: ".$tsname."<br>";
       echo $finaltsdata;
   }
  }
 }
?>

And its respective output:

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

So in the case of this output I wouldn't want the following categories to be displayed because they either don't have a data value or their data value equals - 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) : 

Thanks so much for your help!

1 Answer 1

2

Skip unwanted items by continue statement

if ($tsnumber == $titlenumber){
       if(empty($tsname) or $tsname === '- TBD -') continue;
       $finaltsdata = "<b>".$titlename." (".$tsnumber.") "."</b>: ".$tsname."<br>";
       echo $finaltsdata;
   }
Sign up to request clarification or add additional context in comments.

2 Comments

I'd say continue; is probably the easiest way to handle it in this case. Under different scenarios, it might be worth "unset()" ing the array elements which match your skip criteria before you hit the loop. But continue def seems the way to go in this case.
Yes. because OP doesn't want such categories to be displayed. I'm not sure he want to change the source array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.