3v4l.org

run code in 300+ PHP versions simultaneously
<?php $fieldNames = [ 'id', 'location', 'blankets', 'sleepingbags' ]; $file = new \SplFileObject( 'collections2.csv', 'rb' ); $file->setCsvControl( ",", "\"", "\\" ); $file->setFlags( \SplFileObject::READ_CSV ); $sumBlankets = []; foreach ( $file as $i => $line ) { $row = array_combine( $fieldNames, $line ); if ( !is_numeric( $row['blankets'] ) ) { throw new Exception("invalid data in 'blankets' field for location $k"); } if ( !is_numeric( $row['sleepingbags'] ) ) { throw new Exception("invalid data in 'sleepingbags' field for location $k"); } $k = $row['location']; if ( !array_key_exists( $k, $sumBlankets ) ) { $sumBlankets[$k] = [ 'blankets' => 0, 'sleepingbags' => 0 ]; } $sumBlankets[$k]['blankets'] += $row['blankets']; $sumBlankets[$k]['sleepingbags'] += $row['sleepingbags']; } $sorted = arsort( $sumBlankets ); foreach ( $sorted as $location ) { echo $location; } ?> $sumBlankets outputs Array ( [Location] => Array ( [blankets] => 0 [sleepingbags] => 0 ) [A Capella Salon] => Array ( [blankets] => 11 [sleepingbags] => 0 ) [Allegan Library] => Array ( [blankets] => 47 [sleepingbags] => 2 ) [Avente] => Array ( [blankets] => 2 [sleepingbags] => 0 ) [Biggby - Paw Paw ] => Array ( [blankets] => 13 [sleepingbags] => 1 ) [Biggby - Plainwell] => Array ( [blankets] => 28 [sleepingbags] => 3 ) [Biggby - Sprinkle] => Array ( [blankets] => 25 [sleepingbags] => 0 ) [Biggby - West Main] => Array ( [blankets] => 10 [sleepingbags] => 0 ) [Biggby - Westnedge 311] => Array ( [blankets] => 9 [sleepingbags] => 0 ) [Biggby - Westnedge BestBuy] => Array ( [blankets] => 4 [sleepingbags] => 0 ) [Biggby - Williamston] => Array ( [blankets] => 27 [sleepingbags] => 10 ) )
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.32, 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Fatal error: Uncaught RuntimeException: SplFileObject::__construct(): open_basedir restriction in effect. File(collections2.csv) is not within the allowed path(s): (/tmp:/in:/etc) in /in/QhELg:10 Stack trace: #0 /in/QhELg(10): SplFileObject->__construct('collections2.cs...', 'rb') #1 {main} thrown in /in/QhELg on line 10
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Fatal error: Uncaught RuntimeException: SplFileObject::__construct(): open_basedir restriction in effect. File(collections2.csv) is not within the allowed path(s): (/tmp:/in:/etc) in /in/QhELg:10 Stack trace: #0 /in/QhELg(10): SplFileObject->__construct('collections2.cs...', 'rb') #1 {main} thrown in /in/QhELg on line 10
Process exited with code 255.
Output for 8.0.13
Fatal error: Uncaught RuntimeException: SplFileObject::__construct(collections2.csv): Failed to open stream: No such file or directory in /in/QhELg:10 Stack trace: #0 /in/QhELg(10): SplFileObject->__construct('collections2.cs...', 'rb') #1 {main} thrown in /in/QhELg on line 10
Process exited with code 255.
Output for 7.3.32 - 7.3.33, 7.4.33
Fatal error: Uncaught RuntimeException: SplFileObject::__construct(collections2.csv): failed to open stream: No such file or directory in /in/QhELg:10 Stack trace: #0 /in/QhELg(10): SplFileObject->__construct('collections2.cs...', 'rb') #1 {main} thrown in /in/QhELg on line 10
Process exited with code 255.
Output for 5.6.38
Fatal error: Uncaught exception 'RuntimeException' with message 'SplFileObject::__construct(): open_basedir restriction in effect. File(collections2.csv) is not within the allowed path(s): (/tmp:/in:/etc)' in /in/QhELg:10 Stack trace: #0 /in/QhELg(10): SplFileObject->__construct('collections2.cs...', 'rb') #1 {main} thrown in /in/QhELg on line 10
Process exited with code 255.

preferences:
185.06 ms | 401 KiB | 203 Q