0

i'm pretty new with PHP. So i find this code on the internet and modify it to what i need,but it seems that it gives me one more value in array ,sometimes.

when csv looks like this
a:1
b:2
c:3
d:4

it's fine but when i got
a:1,2,3,4
b:1,2,3,4
c:1,2,3,4
d:1,2,3,4

it adds one more empty line. why is that and can i prevent that?

<?php
$csv_mimetypes = array(
    'text/csv',
    'text/plain',
    'application/csv',
    'text/comma-separated-values',
    'application/excel',
    'application/vnd.ms-excel',
    'application/vnd.msexcel',
    'text/anytext',
    'application/octet-stream',
    'application/txt',);

 if ($_FILES["csv"]["error"] > 0)
{
 echo "<script type='text/javascript'>alert('no file');</script>";
}
elseif(in_array ($_FILES["csv"]["type"] ,$csv_mimetypes))
{
//get the csv file
    $file = $_FILES['csv']['tmp_name'];    
    $csv= file_get_contents($file);
     $array = explode("\r\n", $csv);
for($i=0;$i<count($array);$i++){
    $array[$i] = explode(';', $array[$i]);
}

    echo "<script type='text/javascript'>alert('OK!');</script>";
}
else
 {
 echo "File must be a .csv";
  }
?>
  <script type="text/javascript" src="Bar.js"></script>
        <script type="text/javascript">
            var myJsarray = <?= json_encode($array); ?> ;
sritno
  • 217
  • 1
  • 4
  • 17
  • The example you provide do not contain any semicolon (`;`) character, however, you are doing `explode(';', $array[$i])`... Plus, what is the actual output versus the expected output? – Yanick Rochon Nov 21 '14 at 15:45
  • the csv files have semicolons,so I guess that's not a problem. i'm outputting the values form the array into the table. – sritno Nov 21 '14 at 15:48

2 Answers2

1

Replace file_get_content with file to read each line into an array.

Example:

// $array = file($file, FILE_IGNORE_NEW_LINES);
$array = array("id;podaci1;podaci2", "grad1;25;21", "grad2;23;28", "grad3;45;32");

array_walk($array, function (& $line) {
   $line = explode(';', $line);
});

$json = json_encode($array);

print_r($json);

Output

[["id","podaci1","podaci2"],["grad1","25","21"],["grad2","23","28"],["grad3","45","32"]]
Community
  • 1
  • 1
Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
0

Are you sure your csv files all terminate the same way?
I mean: I suppose that when you get one additional empty line at the bottom of your array, the corresponding csv file terminates with an additional field separator (;) or line separator (\r\n)...

MarcoS
  • 17,323
  • 24
  • 96
  • 174
  • the csv files are made in excel,so i guess they are. – sritno Nov 21 '14 at 15:34
  • excel...made a table..save as csv..? – sritno Nov 21 '14 at 15:45
  • O.k. Please open a file which gives a good array and one which gives a wrong one (using your preferred text editor). Then check if they terminate the same way: I suspect one will have an additional `;` or `\r\n`... It's not due to a bug in MS Excel, but to the way you did create them... – MarcoS Nov 21 '14 at 15:50
  • i did..they look like this working one aaa;46 bbb;23 ccc;51 ddd;23 the one that dosn't work id;podaci1;podaci2 grad1;25;21 grad2;23;28 grad3;45;32 – sritno Nov 21 '14 at 15:52
  • Did you doublecheck for an extra empty line at the end of file that doesn't work? – MarcoS Nov 21 '14 at 15:53
  • i have more files like the exemple i give you and they all are not working – sritno Nov 21 '14 at 16:05
  • The fact your issue is solved by `file($file, FILE_IGNORE_NEW_LINES);`, as in the accepted answer - proves some of your files (the "NOT WORKING" ones) had some trailing new lined you were not aware of. – MarcoS Nov 22 '14 at 15:24