Asked  7 Months ago    Answers:  5   Viewed   40 times

I'm trying to parse a CSV string to an array in PHP. The CSV string has the following attributes:

Delimiter: ,
Enclosure: "
New line: rn

Example content:

"12345","Computers","Acer","4","Varta","5.93","1","0.04","27-05-2013"
"12346","Computers","Acer","5","Decra","5.94","1","0.04","27-05-2013"

When I try to parse it like this:

$url = "http://www.url-to-feed.com";
$csv = file_get_contents($url);
$data = str_getcsv($csv);
var_dump($data);

The last and first element are concatenated in one string:

[0]=> string(5) "12345"
...
[7]=> string(4) "0.04"
[8]=> string(19) "27-05-2013
"12346""

How can I fix this? Any help would be appreciated.

 Answers

23

Do this:

$csvData = file_get_contents($fileName);
$lines = explode(PHP_EOL, $csvData);
$array = array();
foreach ($lines as $line) {
    $array[] = str_getcsv($line);
}
print_r($array);

It will give you an output like this:

Array
(
    [0] => Array
        (
            [0] => 12345
            [1] => Computers
            [2] => Acer
            [3] => 4
            [4] => Varta
            [5] => 5.93
            [6] => 1
            [7] => 0.04
            [8] => 27-05-2013
        )

    [1] => Array
        (
            [0] => 12346
            [1] => Computers
            [2] => Acer
            [3] => 5
            [4] => Decra
            [5] => 5.94
            [6] => 1
            [7] => 0.04
            [8] => 27-05-2013
        )

)

I hope this can be of some help.

Wednesday, March 31, 2021
 
fardjad
answered 7 Months ago
42

When i needed to work with files that big I always use the 'divide and conquer' premise. For your case I would:

  • Dynamicaly create a folder
  • Copy this big file inside it
  • Split it (on linux split called from php) split command
  • use the shell_exec command in php
  • After split it, delete it (the big file)
  • Then loop through the files in the folder reading one by one.
  • And for every file I finish I delete it. So if the time limit occurs you will need just to continue reading the files left in the folder.
  • Wednesday, March 31, 2021
     
    FWH
    answered 7 Months ago
    FWH
    17

    You could use preg_match_all to get an array of keys, and an array of values, then combine them into an associative array using array_combine:

    $str = "var=test;path=test.html";
    preg_match_all("/([^;=]+)=([^;=]+)/", $str, $matches);
    $result = array_combine($matches[1], $matches[2]);
    
    Wednesday, March 31, 2021
     
    Webroots
    answered 7 Months ago
    89

    Ive downloaded the files again and started from scratch and its working. Must have been something silly I had done, sorry for wasting your time and thanks for the help.

    Wednesday, March 31, 2021
     
    Raef
    answered 7 Months ago
    67

    Use str_split http://www.php.net/manual/en/function.str-split.php

    Friday, July 30, 2021
     
    Rocket
    answered 3 Months ago
    Only authorized users can answer the question. Please sign in first, or register a free account.
    Not the answer you're looking for? Browse other questions tagged :