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

时间:2014-07-16 16:31:05

标签: php foreach associative-array strtok

我得到一个无限循环。我相信我在同一行输入中多次读取而不是进入下一行。从我能想出的来看,我需要在while循环结束时移动到下一个部分的另一段代码。我需要用strtok做这个,我的问题是怎么做的?

必须使用前进循环,不得爆炸/暗示。

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

2 个答案:

答案 0 :(得分:0)

这导致你的无限循环:

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

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

答案 1 :(得分:0)

一个可行的解决方案......

<?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);
?> 

更改条件以评估名称值。

Delimeter令牌也是空格,你读取不同的字段,它也必须在结束时阅读。

有点输出格式,瞧...