0

I am fairly new to php and got this from a stack overflow question and cant seem to get it to work for me.

<?php
if (($handle = fopen("fullbox.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    echo "<pre>".print_r($data)." <br /></pre>";
}
fclose($handle);
}
echo $data[1];
?>

I need to set up a couple arrays for pricing using a large number of products(which I can pull from my csv file) Fullbox.csv consists of just 10 numbers with 2 decial places. I have just been trying to test this and this is the output that I get:

Array ( [0] => 8.53 )

Array ( [0] => 4.74 )

Array ( [0] => 5.00 )

Array ( [0] => 2.50 )

Array ( [0] => 6.48 )

Array ( [0] => 3.99 )

Array ( [0] => 8.53 )

Array ( [0] => 4.74 )

Array ( [0] => 8.53 )

Array ( [0] => 4.74 )

Why is the array setting all values in the [0} place holder of the array and also why is there a 1 in between each line. Thanks in advance.

Update If this is a ten item array this should then return the value of the 1 entry in the array but it doesn't. I essentially just need to store all of the prices so I can use them as variables later. I don't need them to be printed. Thanks again.

echo $data[1];
7
  • 2
    The reason you get the 1 is because print_r isn't meant to be echoed, and it's just returning the boolean true, which is interpreted as 1. Commented Jul 12, 2012 at 14:05
  • 2
    Please post the output as text, not as an image. It would also help to see the CSV file you're parsing. Commented Jul 12, 2012 at 14:05
  • use print_r($data, true) for returning the print_r value Commented Jul 12, 2012 at 14:05
  • php.net/manual/de/function.print-r.php Commented Jul 12, 2012 at 14:08
  • If you have an array with only one entry at index 0, why are you trying to access index 1? Commented Jul 12, 2012 at 14:41

4 Answers 4

2

print_r does not return the formatted array by default; it outputs it and returns 1. Pass true as a second parameter.

echo "<pre>".print_r($data, true)." <br /></pre>";

All the values are in the 0 index of the array because arrays start at 0. There is nothing wrong there.

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

2 Comments

If the array starts at 0 should it not end at 9 then since it consists of ten items?? How can I call the values independently if they are all set in the initial spot zero. echo$data[1]; returns nothing.Thanks again.
@Djacksway: Instead of using print_r to display them each time, append them to an array.
1

Try this:

$f = fopen(yourfile, "r");
$fs = filesize(yourfile);
$content = fread($f, $fs);

$lines = explode("\n", $content);
$result = array();
foreach($lines as $line) $result[] = explode(",", $line);

3 Comments

So instead of using a built-in function specifically designed to parse CSV, the OP should ditch that and use a half-baked custom parser?
:-) sometimes it's easier to understand if you are new in php, if you get a hint, how something works inside
You get a +1 from me if you can explain why this works but fgetcsv() doesn't.
0

CSV as in Comma Seperated-Values is an array each line. So each line -even if it has only 1 value- is array.

Also you don't need to echo "print_r".

Comments

0

you also read the data from the CSV using the following function and sort using the usort() function.

 Read data from the CSV

function readCSV() {
 $csv = array_map('str_getcsv', file('data.csv'));
 array_shift($csv); //remove headers
 $csvData = splitandSort($csv);
 createCSV($csvData);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.