简体   繁体   中英

PHP: values assigend to array are truncated to the first letter

Strange behaviour in values assignement for an array. The PHP script reads a csv file and fills an array variable with the values read from the file.

I put an echo command in the while cycle in order to see the first 8 lines read:

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    // numero di elementi presenti nella riga letta
    $num_elementi = count($data); if($nn<8) echo "<br>-- nn:".$nn;
    // popolamento dell'array
    for ($x=0; $x<$num_elementi; $x++) {
        $csvarray[$nn][$x] = $data[$x]; if($nn<8) echo " - ".$csvarray[$nn][$x];
    }
    $nn++;
}

The result is truncated at the first characters:

> -- nn:0 - A - A - W - F - 1 - - 1 - 0 - - 0
> -- nn:1 - A - A - W - F - 1 - 5 - 7 - 6 - 3 - 5
> -- nn:2 - A - A - W - M - 3 - 4 - 3 - 1 - 2 - 1
> -- nn:3 - A - A - W - M - 3 - 6 - 3 - 7 - 4 - 7
> -- nn:4 - A - A - W - M - 1 - 3 - 1 - 7 - 2 - 7
> -- nn:5 - A - A - W - M - 2 - 1 - 2 - 6 - 1 - 6
> -- nn:6 - A - A - W - M - 3 - 3 - 3 - 8 - 1 - 8
> -- nn:7 - A - A - W - F - 2 - 6 - 2 - 1 - 3 - 1

If I print the $data variable (the difference with the previous code is the second echo command)

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    // numero di elementi presenti nella riga letta
    $num_elementi = count($data); if($nn<8) echo "<br>-- nn:".$nn;
    // popolamento dell'array
    for ($x=0; $x<$num_elementi; $x++) {
        $csvarray[$nn][$x] = $data[$x]; if($nn<8) echo " - ".$data[$x];
    }
    $nn++;
}

I get the correct result:

-- nn:0 - AFGHANISTAN - AFGHANISTAN - WIS GREY ROUTE - FIXED - 1 - - 1 - 0 - - 0
-- nn:1 - AFGHANISTAN - AFGHANISTAN - WIS SARL - FIXED - 104 - 55.2599833333333 - 78 - 61 - 3315.599 - 59
-- nn:2 - AFGHANISTAN - AFGHANISTAN AWCC MOBILE - WIS SARL - MOBILE - 36 - 48.57 - 32 - 10 - 2914.2 - 10
-- nn:3 - AFGHANISTAN - AFGHANISTAN ETISALAT MOBILE - WIS SARL - MOBILE - 36 - 6.7854 - 36 - 7 - 407.124 - 7
-- nn:4 - AFGHANISTAN - AFGHANISTAN MOBILE - WIS SARL - MOBILE - 16 - 37.5628333333333 - 14 - 7 - 2253.77 - 7
-- nn:5 - AFGHANISTAN - AFGHANISTAN MTN MOBILE - WIS SARL - MOBILE - 26 - 16.7021166666667 - 26 - 6 - 1002.127 - 6
-- nn:6 - AFGHANISTAN - AFGHANISTAN ROSHAN MOBILE - WIS SARL - MOBILE - 31 - 32.93085 - 30 - 8 - 1975.851 - 8
-- nn:7 - ALBANIA - ALBANIA - WHITE LABEL (WIS) - FIXED - 2495 - 6269.08833333333 - 2451 - 1416 - 376145.3 - 1413

It's just an assignment, why I get different results?

Put a var_dump of csvarray at the end of the loop:

echo "<br>VARDUMP = "; var_dump($csvarray[0]);

The result is:

VARDUMP=string(26) "AAWF1100omunicazioni SpA"

在第6行,您必须使用以下代码:

$csvarray[$nn][$x] = $data[$x]; if($nn<8) echo " - ".$csvarray[$nn]; 

Solved initializing the array with $csvarray=Array(Array()) at the beginning of the code. The array is used before, it's not empty when I use it again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM