Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am almost embarrassed to ask this, but I've been trying to accomplish this task for a few hours now. Without a thorough grasp of the fopen or fgetcsv functions, I'm a bit lost. Each example I find does not quite work for me.

I'm seeking a way to load each line of a CSV file into one array. For example, if this is my CSV file:

Apple,Banana,Orange
Kiwi,Watermelon,Pineapple
Dog,Cat,Bird

Then this would be my array:

Array
(
    [0] => Apple,Banana,Orange
    [1] => Kiwi,Watermelon,Pineapple
    [2] => Dog,Cat,Bird
)

I'd appreciate any tips. Thanks! :)


Just for reference, this is what I have so far:

$list = '../reports/apples.csv';

$csvfile = fopen($list,'rb');
while(!feof($csvfile)) {
$listofthings[] = fgetcsv($csvfile);
}
fclose($csvfile);

print_r($listofthings);

However, this is producing a multidimensional array as follows, when I'd rather just have one big array.

Array
(
    [0] => Array
        (
            [0] => Apple
            [1] => Banana
            [2] => Orange
        )

    [1] => Array
        (
            [0] => Kiwi
            [1] => Watermelon
            [2] => Pineapple
        )

    [2] => Array
        (
            [0] => Dog
            [1] => Cat
            [2] => Bird
        )
)
share|improve this question

3 Answers 3

up vote 2 down vote accepted

If you want to break the file up by lines, use file.

$newArray = file('/path/to/file.csv');

If you want each comma-delimited value as an element of the array, use file_get_contents and explode by a comma.

$contents = file_get_contents('/path/to/file.csv');
$newArray = explode(',', $contents); 
share|improve this answer
    
Oh, you want each line it's own section... updated. –  Nicholas Pickering Feb 23 '13 at 4:32
1  
I noticed the same thing when you first answered and Googled the solution. "\n" worked. Then I came and saw you answered with the same thing! Hah. Marking as solution - this worked for me, and I knew it was going to be something extremely simple. Thanks! –  Jon Feb 23 '13 at 4:36

You could explode after reading the file as Nicholas suggests or you could hit it all at once with file:

$lines = file('/path/to/file.csv');
share|improve this answer
    
+1: Very nice. I didn't know this. –  Nicholas Pickering Feb 23 '13 at 4:40
1  
Wish, I could vote up again - I think you've changed my life. –  Nicholas Pickering Feb 23 '13 at 4:50
1  
Hahaha... yeah, the other nice thing is it takes the detect line endings setting into account, so you dont have to worry as much about differing line endings. –  prodigitalson Feb 23 '13 at 21:29

are looking for this ??

$mytext = "Apple,Banana,Orange
Kiwi,Watermelon,Pineapple
Dog,Cat,Bird";

$line_explode = explode("\n", $mytext);
$comma_explode = array();
foreach ($line_explode as $line) {
  $comma_explode[] = explode(",", $line);
}

print_r($comma_explode);

Output

Array
(
    [0] => Array
        (
            [0] => Apple
            [1] => Banana
            [2] => Orange
        )

    [1] => Array
        (
            [0] => Kiwi
            [1] => Watermelon
            [2] => Pineapple
        )

    [2] => Array
        (
            [0] => Dog
            [1] => Cat
            [2] => Bird
        )

)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.