Reputation: 457
I can not get this PHP array to return any values for me. The CSV sheet consists of 10 numbers. The only output I get is "array
"
$data = array();
if (($handle = fopen("fullbox.csv", "r")) !== FALSE) {
while(($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
$data[] = $row;
}
}
echo $data[1];
Any help would be greatly appreciated.
Upvotes: 1
Views: 157
Reputation: 270677
Your code is already correct.
$data
is multidimensional array, each element is an array itself. When you echo $data[1]
you are asking PHP to write a string representation of a more complex array variable, which PHP handles by outputting array
instead of its contents.
Instead, var_dump()
it to see what it contains.
var_dump($data);
A single value would be accessed via something like :
echo $data[1][0];
Edit after comment:
If the CSV contains only one value per row, access it directly via $row[0]
when appending to the output array in order to get it as a 1D array:
if (($handle = fopen("fullbox.csv", "r")) !== FALSE) {
while(($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
$data[] = $row[0];
}
}
// $data is now a 1D array.
var_dump($data);
Upvotes: 3
Reputation: 1010
$data[1] is the second element in your $data variable ($data[0] would be the first).
You are getting an array back instead of a value which is why PHP is echoing the word array.
This should work better.
$data[0][0]
Your $data variable is a multidimensional array and you are only going one level deep.
Upvotes: 0
Reputation: 6005
Thats because $data[1] is an array of the 10 numbers in the CSV row, so $data is now a multi-dimensional array. echo $data[1][0]
will output one of the numbers
Upvotes: 0