/    Sign up×
Community /Pin to ProfileBookmark

parsing selected non sequential columns from csv

I am very newbie to php.I am upto parse the csv file and want to copy selected columns to other csv file, which are not sequential. I have read data from csv file in multidimentional array. so I have all key value pairs, where keys are name of columns and values are the data from csv file. I am using

foreach($contents as $line) {
$line = preg_replace( “/r|n/”, “”, $line );
$line_array = explode(“,”,$line);
if($i==0)
{
foreach($line_array as $key=>$val)
{
$csv_heading[$key] = trim($val);
}
}
else
{
foreach($line_array as $key=>$val)
{
$csv_array[$i][$csv_heading[$key]] = $val;
}
}
$i++;
}
$len = count($csv_array);
$row = array();
for($col = 0 ; $col < $len ; $col++)
{
if(array_intersect_key($col , $picked))
$row[] = $csv_array[$col];
$final = $row;
print_r($row);
}

and my csv file contains records like

name,city,state,country
Dave,mumbai,maharashtra,India

and so on. from the csv file I want to copy the columns name and city to other csv. can anyone help me? Thanks in advance.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@newbiePHPauthorOct 02.2015 — No help??
Copy linkTweet thisAlerts:
@ginerjmOct 02.2015 — Not a very clear explanation of your dilemma. Have to wonder why one would want to move data from one csv file to another one instead of making a more-usable database table out of it.

Am I understanding you in that your csv is a set of column names and their values? That is what it appears to be, which is an unusual csv format.

Perhaps you could post a sample of ONE row of your csv and ONE row of your desired csv files, IF that is really your best alternative.
Copy linkTweet thisAlerts:
@NogDogOct 02.2015 — It would be simpler and more memory efficient (PHP arrays are memory hogs) to just read from one file and write to the other, line by line:
[code=php]
$fhIn = fopen('path/to/source_file.csv', 'r');
$fhOut = fopen('path/to/output.csv', 'w'); // should do some error-checking for both of these!

while(($row = fgetcsv($fhIn)) != false) {
fputcsv($fhOut, array_slice($row(0, 2));
}

fclose($fhIn);
fclose($fhOut);
[/code]
Copy linkTweet thisAlerts:
@newbiePHPauthorOct 02.2015 — thanx for ur reply @ginerjm.

I have requirment dat is only why i want to copy in diff csv.


thanx for ur reply @NogDog.

but array_slice works horizontly.not vertically.I want columns to be copied.
Copy linkTweet thisAlerts:
@ginerjmOct 02.2015 — Good luck with this cause you're not making much sense.
Copy linkTweet thisAlerts:
@NogDogOct 02.2015 — thanx for ur reply @ginerjm.

I have requirment dat is only why i want to copy in diff csv.


thanx for ur reply @NogDog.

but array_slice works horizontly.not vertically.I want columns to be copied.[/QUOTE]


Afraid I don't understand what you're trying to do, then (or I do, but you don't understand my code ? ). What I suggested would read this:
<i>
</i>11,12,13,14
21,22,23,24
31,32,33,34
...and output this:
<i>
</i>11,12
21,22
31,32

If it should do something else, then it might help to provide a similarly brief example of input and expected output.
×

Success!

Help @newbiePHP spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.30,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...