【发布时间】: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