fgetcsv() How to get two rows at a time

I have CSV file with 1000s of rows and two columns.

I have a script that echos one row at a time (both columns) using this code:

[php]if( ($handle = fopen($filename, “r”)) !== FALSE )
{
$columns = fgetcsv($handle, 4096,",", ‘"’ );

if (!$columns) {
    $error['message'] = 'Empty';
    return ($error);
}

while( ($row = fgetcsv($handle, 4096,",", ‘"’ )) !== FALSE )
{
echo $row[0];
echo $row[1];
}
}[/php]

$row[0]; gives me row 1, column 1
$row[1]; gives me row 1, column 2

It works really nicely. However, I can’t seem to figure out how to obtain two rows at a time and separate them using a dash -----.

For example,

when I echo $row[0], I want it to contain:

Content of ROW 1, COLUMN 1
---- <-- with these dashes
content of ROW 2, COLUMN 1

AND when I echo $row[1], I want it to contain:

Content of ROW 1, COLUMN 2
---- <-- with these dashes
content of ROW 2, COLUMN 2

Any thoughts?

I don’t understand what you are asking to do. Can you post an image of your desired output with dummy data in it?

You may need to read the entire file into an array before echoing it but with the detail as above there may be another way.

"That isn't how it works. That isn't how any of this works"

Let’s get creative! I have some idea’s, but an output example would be more helpful.

I’ll make it easier to understand. Let’s pretend we have and Excel file that’s 4 rows by 2 columns.

When I execute the while statement:

[php]while( ($row = fgetcsv($handle, 4096,",", ‘"’ )) !== FALSE )
{
print_r($row);
}[/php]

I get:


Array
(
    [0] => blah blah blah
    [1] => he he he
)
Array
(
    [0] => bleh bleh bleh
    [1] => ha ha ha
)
Array
(
    [0] => bloh bloh bloh
    [1] => ho ho ho
)
Array
(
    [0] => bluh bluh bluh
    [1] => hu hu hu
)

I’d like to combine every 2 arrays so that we end up with:


Array
(
    [0] => blah blah blah - - - bleh bleh bleh
    [1] => he he he - - - ha ha ha
)
Array
(
    [0] => bloh bloh bloh - - - bluh bluh bluh
    [1] => ho ho ho - - - hu hu hu
)

So, when I execute the while statement,

[php]while( ($row = fgetcsv($handle, 4096,",", ‘"’ )) !== FALSE )
{
echo $row[0] . $row[1];
}[/php]

I only get two out puts instead of the original 4.

I think the biggest issue is how the data is being brought in. If we are using a xls file that means that the data is coming in staggered, rather than in row form, which makes this an ETL process…

Give me a minute, I’m bored.

Thanks.

All the CVS files I’m working with are structured n by 2, where n can be anywhere up to 100,000 rows.

Is this an all the time or a off thing? Will you ALWAYS want the even and odd rows togo together?

Yes, so if I run the script, it will combine every two rows.

For example, a file containing 100 rows will produce 50 outputs instead of 100.

https://www.ideone.com/uorCW0

I didn’t have the file, but this should give you a starting point on how to do so.

That’s incredible. But I can’t seem to get it to work.

How would I incorporate that into my while statement?

So far I have this:

[php]if( ($handle = fopen($filename, “r”)) !== FALSE )
{
$columns = fgetcsv($handle, 4096,",", ‘"’ );

if (!$columns) {
	$error['message'] = 'Empty';
	 return ($error);
}

$key = [];
$val = [];

while( ($row = fgetcsv($handle, 4096,",", '"' )) !== FALSE ) 
{		 
	 for($i=0; $i<count($row); $i++)
	 {
		array_push($key,$row[$i][0]);
		array_push($val,$row[$i][1]);
	 }
	 //print_r($val);
	 // print_r($key);
	 
	 for($index=0; $index < count($key); $index++)
	 {
		echo "{$key[$index]} --- {$key[$index+1]}\n";
		echo "{$val[$index]} --- {$val[$index+1]}\n";
		$index++;
	 }

}	

}[/php]

The CSV file I’m using can be found here: https://expirebox.com/download/951f7c4f09b45194bd6791e858fb5870.html

Just define your $filename as $filename = ‘/path_to/test.csv’;

[php]
$counter = 0;
while( ($row = fgetcsv($handle, 4096,",", ‘"’ )) !== FALSE )
{

		array_push($key,$row[$counter][0]);
		array_push($val,$row[$counter][1]);
}	

	 for($index=0; $index < count($key); $index++)
	 {
		echo "{$key[$index]} --- {$key[$index+1]}\n";
		echo "{$val[$index]} --- {$val[$index+1]}\n";
		$index++;
	 }

[/php]

What is the REAL problem you are trying to solve. Not your attempt at solving it. This is clearly an XY Problem.

Sponsor our Newsletter | Privacy Policy | Terms of Service