简体   繁体   中英

Trying to use foreach loop inside while loop, keep getting infinite loop

I'm still very new to PHP, so I was wondering if I could get some help? I'm supposed to create a chart by iterating through an associative array using a while loop with a foreach loop inside it. But I keep getting an infinite loop and I'm not entirely sure how to fix it. I also suspect that the if statement conditions aren't helping, but I don't know either. What am I missing and is there a better way to go about this?

Here's the code in question:

<?php
 include 'davesinventory.php';
 $delimiter = " \n";
 $inventory['name'] = rtrim(strtok($data, $delimiter));
 $inventory['year'] = rtrim(strtok($delimiter));
 $inventory['serial'] = rtrim(strtok($delimiter));
 $inventory['seats'] = rtrim(strtok($delimiter));
 $inventory['charge'] = rtrim(strtok($delimiter));
 $inventory['days'] = rtrim(strtok($delimiter));
 $inventory['rev'] = rtrim(strtok($delimiter));
 $inventory['orig'] = rtrim(strtok($delimiter));
 $inventory['miles'] = rtrim(strtok($delimiter));
 $inventory['deprec'] = rtrim(strtok($delimiter));
 $inventory['freq'] = rtrim(strtok($delimiter));

 printf("<b> VEHICLE\tYEAR\t SERIAL#\t RENT COST\t DAYS OUT\t REVENUE\t ORIG. PRICE\t MILEAGE\t NOTES\n</b>");
?>
<hr>
<?php
 while($inventory['name']){
   foreach($inventory as $key => $vehicles){
         if ($key !== ['seats'] || ['deprec'] || ['freq'])
          {
             print $vehicles;
          }
          else if($key == ['seats'] || ['deprec'] || ['freq'])
          {
            echo " ";
          }
   }
}
?>
<pre>

Here is the file that the 'include' calls for:

<?php
$data =
"DodgeAvenger 2006 DA111-9 4 35.50 105 3727.50 21297.00 8795 .20 Monthly
Olds_Alero 2004 OA340-1 5 29.95 126 3773.70 23335.00 36010 .20 Monthly
Chry_PT_Crsr 2003 CPTC-MW2 4 37.95 26 986.70 15405.00 29020 .20 Weekly
Cadillac_Limo 1999 1999-01 18 142.50 4 570.00 38900.00 187419 .10 Weekly
Chev_M_Carlo 1997 CMC-21 6 27.30 55 1501.50 19437.50 113689 .20 Monthly
Chev_Suburban 1997 CSB-011 8 42.75 17 726.75 29999.00 137560 .20 Monthly
VW_Bus_T2A 1977 VWB-09 32 15.00 16 240.00 12000.00 397800 NA Daily
Ford_Stn_Wgn 1976 FSW-67 8 10.95 6 65.70 9899.00 149379 NA Whenever
Toy_Forklift 1997 6FGCU-45 1 61.25 65 3981.25 8795 732 .10 Yearly
Cat_Dozer(D7H) 1989 1989-11 1 98.00 5 490.00 67850.00 1304 .10 Yearly
";
?>

No need to use while loop here. Foreach is enough

<?php    
    if(isset($inventory['name'])){
        foreach($inventory as $key => $vehicles){
            if ($key !== 'seats' || $key !=='deprec' || $key !=='freq')
            {
               print $vehicles;
            }
            else{
               echo " ";
            }
        }
    }
?>

Blank's answer should work for you. Mine should actually be a comment but it's much easier to format code in an answer.

If using while is just an annoying requirement for a homework or test (according to your comment...) you can simply work around it:

$finished = false;

while (!$finished) 
{
    //do all the work here - foreach loop and so on

    $finished = true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM