Question

I have a csv with two rows.

if (($handle = fopen("twoRows.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
print_r($data);

This returns two arrays with a line break at the end of the first array.

If I want to access the first column, I do

echo $data[0];

And get

value1
value2

Yet, if I do something like:

$data[0] = $post_ID;
echo 'Post ID = ' . $post_ID . '<br />';

I get zero output on $post_ID

Post ID =
Post ID =

How can I perform a function on each first column value?

Was it helpful?

Solution

where is $post_ID declared?

Don't you mean to do

$post_ID = $data[0];

echo 'Post ID = ' . $post_ID . '<br />';

OTHER TIPS

You're setting the $data[0] to $post_ID, it should be the other way around:

$post_ID = $data[0];
echo 'Post ID = ' . $post_ID . '<br />';

Do it inside your loop:

if (($handle = fopen("twoRows.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $post_ID = $data[0];
        echo 'Post ID = ' . $post_ID . '<br />';
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top