0
$arr=array();
$row = -1;
if (($handle = fopen("out.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);

        $row++;
        for ($c = 0; $c < $num; $c++) {
            $arr[$row][$c]= $data[$c];
        }
    }
    fclose($handle);
}

I'm using this code to read excel file data, this code counts elements in a row that are divided by comma (,),

Name, Surname, Num, Tel

  1. Name
  2. Surname
  3. Num
  4. tel

but in one field I have word Orginal, and this code also divides element by that word like this:

  1. Orgina
  2. l

and in that way, I receive wrong elemnts, any help?

5
  • Can you show the actual file you are trying to read with the content causing the problem so others can reproduce the issue. Commented Dec 17, 2019 at 7:00
  • Show the picture of this part of file, perhaps there's a problem inside this row, not in code. Commented Dec 17, 2019 at 7:01
  • BTW - you could replace the entire code in the loop with $arr[] = $data; if you just want to copy all of the array. Commented Dec 17, 2019 at 7:04
  • how i can upload a file Commented Dec 17, 2019 at 8:04
  • @NigelRen i updated description. I insert row how it looks in editor Commented Dec 17, 2019 at 9:34

1 Answer 1

1

In the line

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

the 1000 is the maximum length of data to read, as your data (in the example posted) has more than that, it will split the data.

To allow it to read the data properly you can just leave the second and third parameters as defaults ( null for length - in other words any length and the default delimiter is a comma anyway...

while (($data = fgetcsv($handle)) !== FALSE) {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.