【问题标题】:How to loop through an external file using an associative array using strtok in php如何在php中使用strtok使用关联数组遍历外部文件
【发布时间】:2014-07-16 16:31:05
【问题描述】:

我得到一个无限循环。我相信我正在多次阅读同一行输入,而不是转到下一行。根据我的想法,我需要在 while 循环末尾上方的另一段代码移动到下一段。我需要使用 strtok 来做到这一点,我的问题是怎么做?

必须使用 FOREACH 循环,不得爆炸/内爆。

“包含文件是 bbs_2013invent.php,内容如下:

<?php
$data =
"Sunscreen_SPF-55 BB-ss1 S 9.98 83 828.34 4.99 NA
Sunscreen_SPF-7000 BB-ss2 S 29.98 9 269.82 19 NA
SunHat-F BB-sh3 S 33.79 41 1385.39 25.5 NA
SunHat-M BB-sh8 S 41.79 18 752.22 29.5 NA
Sandals BB-sd4 S 19.38 32 620.16 4.75 NA
Towel-Large BB-tl S 45 57 2565.00 29.99 NA
Towel-Indiv BB-ts S 11.75 133 1562.75 5.9 NA
Beach_Chair BB-bc2 S 67.5 19 1282.50 47.5 NA
Umbrella BB-umb R 129 18 2322 398 0.4
Surfboard BB-surf2 R 135 27 3645 735 0.2
JetSki BB-jsk7 R 160.25 38 6089.50 18795.00 0.4
Outlaw20_Powerboat BB-pb20 R 298 11 3278 67850.00 0.3";
?>

其余代码如下:

<?PHP    
//Imports a file
include 'bbs_2013invent.php';
//variable
$delimiters = "\n";

//Declare and fill array
$product = ['name'    => rtrim(strtok($data, $delimiters)),
       'serial'   => rtrim(strtok($delimiters)),
       'status'   => rtrim(strtok($delimiters)),
       'price'    => rtrim(strtok($delimiters)),
       'units'    => rtrim(strtok($delimiters)),
       'revenue'  => rtrim(strtok($delimiters)),
       'cost'     => rtrim(strtok($delimiters)),
       'factor'   => rtrim(strtok($delimiters)),
       ];

//Column headers
printf("PRODUCT  \tSERIAL#  \tPRICE/RENTALCHG   \tUNITS/DAYS    \tREVENUE   \tORIGCOST  \tNOTES");
//while loop
while(['name'])
{
//Calculates the total days rented and revenue respectively
$totalDays += $units;
$totalRevenue += $revenue;
if ($key == 'status' && $value = S && "units" <800)
{
  printf("\t L");
}
if ($key == 'status' && $value = R && "units" <20)
{
  printf("\t LR");
}

//foreach loop
foreach($product as $key => $value)
{
printf($key, $value);
}

} //end of while loop
//displays the total number of days items have been rented
printf($totalDays);
//displays the total revenue of all products combined
printf($totalRevenue);
?> 

【问题讨论】:

    标签: php foreach associative-array strtok


    【解决方案1】:

    这导致你的无限循环:

    while(['name']){ ... }
    

    这个循环一直运行到['name'] 评估为 FALSE,这永远不会发生。

    【讨论】:

      【解决方案2】:

      一种可能的解决方案...

      <?PHP    
      //Imports a file
      include 'data.php';
      //variable
      $delimiters = " \n";
      $totalDays = $totalRevenue = 0.0;
      //Declare and fill array
      $name = rtrim(strtok($data, $delimiters));
      
      $spaces = ['name'    => "-20",
              'serial'   => "-10",
             'status'   => "-6",
             'price'    => "10",
             'units'    => "10",
             'revenue'  => "15",
             'cost'     => "15",
             'factor'   => "8"
      ];
      
      //Column headers
      printf("PRODUCT\t\t    SERIAL#\t PRICE/RENTALCHG  UNITS/DAYS\tREVENUE\t\tORIGCOST   NOTES\n");
      //while loop
      while($name)
      {
      
      $product = ['name'    => $name,
              'serial'   => rtrim(strtok($delimiters)),
             'status'   => rtrim(strtok($delimiters)),
             'price'    => rtrim(strtok($delimiters)),
             'units'    => rtrim(strtok($delimiters)),
             'revenue'  => rtrim(strtok($delimiters)),
             'cost'     => rtrim(strtok($delimiters)),
             'factor'   => rtrim(strtok($delimiters)),
             ];
      
      //Calculates the total days rented and revenue respectively
      $totalDays += $product["units"];
      $totalRevenue   += $product["revenue"];
      
      //foreach loop
      foreach($product as $key => $value)
      {
      if ($key == 'status' && $value = "S" && "units" <800)
      {
        printf("%${spaces[$key]}s","L");
      } else if ($key == 'status' && $value = "R" && "units" <20)
      {
        printf("%${spaces[$key]}s","LR");
      } else {
      printf("%${spaces[$key]}s",  $value);
      }
      
      }
      
      print "\n"; 
      
      $name = rtrim(strtok($delimiters));
      
      } //end of while loop
      //displays the total number of days items have been rented
      printf("\n%10.02f",  $totalDays);
      //displays the total revenue of all products combined
      printf("\n%10.02f", $totalRevenue);
      ?> 
      

      更改条件以评估名称值。

      分隔符标记也是空格,你读取不同的字段,它也必须在 while 结束时读取。

      有点输出格式,瞧……

      【讨论】:

        猜你喜欢
        • 2014-09-01
        • 2017-06-07
        • 1970-01-01
        • 1970-01-01
        • 2012-06-26
        • 1970-01-01
        • 1970-01-01
        • 2012-07-06
        • 1970-01-01
        相关资源
        最近更新 更多