4

I have string like which is created using fputcsv

Date,Name,Hours 2013-01-02,"Test User",7:59 2013-01-03,"Test User",7:53 2013-01-04,"Test User",8:12 2013-01-07,"Test User",7:56 2013-01-08,"Test User",8:25 2013-01-09,"Test User",7:56 2013-01-10,"Test User",8:10 2013-01-11,"Test User",7:53 2013-01-14,"Test User",7:54 2013-01-15,"Test User",0:34 2013-04-01,"Test User",5:51 2013-04-02,"Test User",8:50 2013-04-03,"Test User",7:25 2013-04-04,"Test User",8:3 2013-04-05,"Test User","10:42:52[Not punch out]" ,Total,103:1

when proper header (header('Content-type: application/csv'); header('Content-Disposition: attachment; filename="'.$filename.'"');) is set it gives correct csv file.

Converting csv files data to an array using php str_getcsv

But I want this string again to be converted to a array format so I tried passing above string to str_getcsv but getting array like this

Array
(
    [0] => Date
    [1] => Name
    [2] => Hours
2013-01-02
    [3] => "Allen Herrera"
    [4] => 7:59
2013-01-03
    [5] => "Allen Herrera"
    [6] => 7:53
2013-01-04
    [7] => "Allen Herrera"
    [8] => 8:12
2013-01-07
    [9] => "Allen Herrera"
    [10] => 7:56
2013-01-08
    [11] => "Allen Herrera"
    [12] => 8:25
2013-01-09
    [13] => "Allen Herrera"
    [14] => 7:56
2013-01-10
    [15] => "Allen Herrera"
    [16] => 8:10
2013-01-11
    [17] => "Allen Herrera"
    [18] => 7:53
2013-01-14......................

Can you help me to get properly formatted array so that I can easily use it to create table looping on the array.

2
  • I think you need to explode() it using PHP_EOL as delimiter and then use each part in str_getcsv() Commented May 17, 2013 at 11:33
  • It seems that you just kept pushing values into your result array one-at-a-tme, but you failed to include your coding attempt in your question and this means your question Needs Debugging Details. Commented Jul 18, 2024 at 9:27

5 Answers 5

5
 $file = file_get_contents("test.csv");
 $data = array_map("str_getcsv", preg_split('/\r*\n+|\r+/', $file));
 print_r($data);
1
  • This will not give me array how I exactly want. Commented May 17, 2013 at 11:44
3

After little searching got solution on this

$data = array_map("str_getcsv", preg_split('/\r*\n+|\r+/', $string_data));
print_r($data);

Giving me array like this

Array
(
    [0] => Array
        (
            [0] => Date
            [1] => Name
            [2] => Hours
        )

    [1] => Array
        (
            [0] => 2013-01-02
            [1] => Test User
            [2] => 7:59
        )

    [2] => Array
        (
            [0] => 2013-01-03
            [1] => Test User
            [2] => 7:53
        )

    [3] => Array
        (
            [0] => 2013-01-04
            [1] => Test User
            [2] => 8:12
        )

    [4] => Array
        (
            [0] => 2013-01-07
            [1] => Test User
            [2] => 7:56
        )

    [5] => Array
        (
            [0] => 2013-01-08
            [1] => Test User
            [2] => 8:25
        )

    [6] => Array
        (
            [0] => 2013-01-09
            [1] => Test User
            [2] => 7:56
        )

    [7] => Array
        (
            [0] => 2013-01-10
            [1] => Test User
            [2] => 8:10
        )...
1
  • I would like to recommend a trick in order to add arguments for str_getcsv function. Try: $contents = Storage::get('folder/file.csv'); $filterCsv = function ($arg) { return str_getcsv($arg, ';', '"', '\r\n'); }; $mapped = array_map($filterCsv, preg_split('/\r*\n+|\r+/', $contents)); Commented Nov 19, 2020 at 19:59
2

That will be something like:

$parts = explode(PHP_EOL, $string);
$array = array();
foreach ($parts as $part) {
    $array[] = str_getcsv($part);
}

print_r($array);
1
  • if PHP_EOL doesn't work, try different delimiters ("\n", "\r\n", "\r"). Commented May 17, 2013 at 11:52
2

I use this way to converting csv files data to an array:-

ini_set('auto_detect_line_endings', TRUE);/// (PHP's detection of line endings) write at the top.


$csvrows = array_map('str_getcsv', file($filepath));
$csvheader = array_shift($csvrows);
$csv = array();
foreach ($csvrows as $row) {
   $csv[] = array_combine($csvheader, $row);
}
-1
 $path = 'uploads/my_csvfile.csv';
 $file_content = file_get_contents($path);
 $matches = array_map('str_getcsv', preg_split('/\r*\n+|\r+/',$file_content));
 print_r($matches);
0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.