PHP数组值不会一起添加

时间:2017-02-09 15:21:34

标签: php arrays

我正在尝试使用PHP解析.txt文件。当我浏览数组时,我试图将索引2和3一起添加。当我做一个简单的打印时,打印出来的值,但是当它们一起添加时它们会导致零。

<?php
$inFile =  'inData2.txt';
$inData = file( $inFile ) or die( "Cannot open file=$inFile");

$totSales = 0;
$totItems = 0;
$firstLine=true;

foreach ( $inData as $ct => $line ){
    if( $firstLine ) {
        $firstLine=false;
        continue;   }

    $spLine = explode( ",", $line );

    $new = intval($spLine[2]) + intval($spLine[3]);
    print "$spLine[2] <br>";
    print "$spLine[3] <br>";
    print "$new <br>";

    $totSales += $spLine[2] * $spLine[3];
    $totItems += 1; }

print "<br /> totItems=$totItems TotSales=$totSales";
?>

.txt文件包含:

Part ,Description,Sales,PricePerUr,Year
B101,10 speed Bike,12,100,2015
B102,15 Speed Mountain,15,200,2015
B103,20 Speed Mountain,10,400,2015
B104,TriCycle,25,50,2016

0 个答案:

没有答案