Link to home
Start Free TrialLog in
Avatar of maccaj51
maccaj51Flag for Afghanistan

asked on

CSV & getting certain values in PHP

Hi Experts,

This question got very comprehensively answered -

The accepted script outputs all rows and columns...

Is there a way of selecting which columns you wish to output?

Many Thanks in advance
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

The short answer is, "Yes."  The longer answer, and I assume you want more than a yes/no response, is going to be dependent on your data set.

The array returned by fgetcsv() is numerically indexed.  If you know the array position of the columns you want, it's easy to get them.  If you know only the "name" (perhaps from the top row of the CSV) you can look up its number and use that number to choose the positions in the rest of the rows.

If you have a sample CSV file and want to look for a specific set of columns by rules, please post the test data and the rules.  I'll try to show you the code you might use.
Avatar of maccaj51


Your saving me alot of bother today Ray - very appreciative...

Heres my sample data:

How would I just get name and age columns for example...

Perhaps using an array with column numbers in?

$columns = 2,3;
You could use the column numbers, but a more generalized solution would be my preference.  I'll post back with something for you in a moment.  Remember, array keys are always numbered from zero. :-)
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ray - You're a legend!

Thanks for your help and have a great weekend!
You, too.  Thanks for the points and thanks for using EE, ~Ray