3v4l.org

run code in 300+ PHP versions simultaneously
<?php function csv_reverse($handle, ...$csv_options) { $offsets = []; do { $offsets[] = ftell($handle); } while($row = fgetcsv($handle, ...$csv_options)); array_pop($offsets); // last offset is EOF for( $i=count($offsets)-1; $i>=0; --$i ) { fseek($handle, $offsets[$i]); yield fgetcsv($handle, ...$csv_options); } } $csv = <<<_E_ id,name,value 1,foo,"hello world" 2,bar,"hello world" 3, baz,"""hello world""" _E_; $in = fopen('php://memory', 'rwb'); fwrite($in, $csv); rewind($in); fgets($in); foreach( csv_reverse($in, null, ',', '"', '\\') as $row ) { var_dump($row); }
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
array(3) { [0]=> string(1) "3" [1]=> string(4) " baz" [2]=> string(14) ""hello world"" } array(3) { [0]=> string(1) "2" [1]=> string(3) "bar" [2]=> string(12) "hello world" } array(3) { [0]=> string(1) "1" [1]=> string(3) "foo" [2]=> string(11) "hello world" }

preferences:
93.1 ms | 407 KiB | 5 Q