如何在php中使用strtok使用关联数组循环遍历外部文件


How to loop through an external file using an associative array using strtok in php

我得到一个无限循环。我相信我在同一行输入中读了很多遍,而不是去读下一行。从我能想到的,我需要另一段代码,就在while循环的末尾,移动到下一段。我需要用strtok来做这个,我的问题是怎么做?

必须使用FOREACH循环,不能使用explosion/IMPLODE。

包含的文件是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);
?> 

这会导致无限循环:

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

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

一个可能的解决方案…

<?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结束时读取。

输出格式,瞧…