How to set column limit in csv file?

Recommended Answers

All 2 Replies

You could use array_chunk(), for example in file.csv you have:

column1,column2,column3,column4,column5
column1,column2,column3,column4,column5
column1,column2,column3,column4,column5

And your script executes a loop:

$file = file('./file.csv');
$result = array();

foreach($file as $csv)
{
    $s = array_chunk(str_getcsv($csv), 3);
    $result[] = $s[0];
}

The $result array will return only the first three columns of each line.

Thank You.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.