0

I am really struggling to work out how to turn certain columns of data from a csv file into an array. At the moment I can convert the whole thing into an array using:

$csv = array();

$lines = file('../data/StaffData.csv', FILE_IGNORE_NEW_LINES);

foreach ($lines as $key => $value)
{
    $csv[$key] = str_getcsv($value);
}

But is there a way to only get certain columns from the array? Such as columns 1 and 2, or 1 to 4? etc.

The goal is to do this so I can put certain data into a table with FPDF. Which I am also struggling with :/

-So if anyone knows how to quickly put a nice looking table into FPDF let me know please. I am trying to use BasicTable function from the site, but my table is going off the page and doesn't look great. Its the first time I have used it, and it is going pretty terribly.

I know this is kind of 2 questions. But the first one (in the title) is the main one to start with. Thanks

2 Answers 2

1

str_getcsv() parses a single row, and you simply need to extract data from the row.

$csv = array();

$lines = file('../data/StaffData.csv', FILE_IGNORE_NEW_LINES);

foreach ($lines as $key => $value)
{
    $row = str_getcsv($value);
    $csv[$key] = array($row[0], $row[1], ..., $row[n]);
}

Where you specify the column numbers you want in array($row[0], $row[1], ..., $row[n]);

Sign up to request clarification or add additional context in comments.

Comments

1

You can use explode() on each line to get the column in an array. Then when you write to $csv, just pick what you want and skip what you don't.

for{
    $row = explode(",", $line);
    $csv[$key]['wantedcol1'] = $row[0];  //first col
    $csv[$key]['wantedcol2'] = $row[2];  //third col
}

This is just a simple little example that is skipping the second column from your file as it's writing $csv.

4 Comments

Thanks for the reply Michael. Sorry for my noobiness, but im not sure what you mean entirely? Do you have an example? Or can point me to an example? Thanks
Dig into the docs for explode(),...you'll use it alot and it's pretty simple. It asks for a delimiter and a string. In your case, put in each line/row and use the commas as your delimter. It'll turn that string into an array. Use print_r() to see what explode() is outputting. You'll be able to pick the columns you want out of that array. Get your hands dirty, you'll be fine.
I have used explode many times before, just not in a situation like this. Maybe I am a little confused? I can make the array, I just want to know how to pick the columns from that array. For it to work in my table generator, it needs to be those certain columns alone and nothing else. I know i can just print certain columns from a full array, is that what your saying? It is multidimensional and uses index for everything. I hope I am making sense? I think I am just having trouble thinking of the algorithm for what your suggesting? Thanks,
I updated my answer,...you just need to declare your $csv a little bit more granularly,...this is what makes it easy to skip or order the columns however you'd like. So by the time $csv is finished in the factory, it'll be exactly how you need it to be.

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.