3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Exercise_One; /** * Zillow exercise one * * Take an associative array and sort it based on columns and directions specified. (PSR1 Standard) * * @author Joe Rocha <joe.rocha@me.com> */ class Singleton { /** * @var Singleton The reference to *Singleton* instance of this class */ protected static $instance; /** * Returns sorted multidimensional, associative array * * @param array $array Data to sort. * @param string $sortKey Key to sort by. * @param string $dir Direction to sort ('ASC' || 'DESC'). * @param bool $nat Natural sorting. * * @return array sorted by sortBy(). */ public static function sortBy( &$array, $sortKey, $dir = 'ASC', $nat = false ) { if ( null === static::$instance ) { static::$instance = new static(); }; function sortLogic( $key, $dir, bool $nat ) { return function( $a, $b ) use ( $key, $dir, $nat ) { if ( true === $nat ){ return strnatcmp( $a, $b )[$key]; } else { return $dir === 'ASC' ? $a[$key] > $b[$key] : $a[$key] < $b[$key]; } }; } uasort( $array, sortLogic( $sortKey, $dir, $nat ) ); return static::$instance; } /** * Make constructor private, so no one can call "new Class". */ private function __construct() {} /** * Make clone magic method private, so no one can clone instance. */ private function __clone() {} /** * Make sleep magic method private, so no one can serialize instance. */ private function __sleep() {} /** * Make wakeup magic method private, so no one can unserialize instance. */ private function __wakeup() {} } // Dummy Data for testing $data = array(); $data[] = array('id' => 1, 'number' => '1', 'street' => 'Battery St', 'unit' => '1', 'rent' => 1200); $data[] = array('id' => 10, 'number' => '10', 'street' => 'Battery St', 'unit' => '3', 'rent' => 1800); $data[] = array('id' => 2, 'number' => '2', 'street' => 'Leavenworth St', 'unit' => '11', 'rent' => 800); $data[] = array('id' => 12, 'number' => '12', 'street' => 'Battery St', 'unit' => '10', 'rent' => 3400); $data[] = array('id' => 1, 'number' => '14', 'street' => 'Leavenworth St', 'unit' => '10', 'rent' => 1450); $data[] = array('id' => 1, 'number' => '01', 'street' => 'Battery St', 'unit' => '5', 'rent' => 1000); // Sanity check Singleton::sortBy( $data, 'number', 'ASC', 'cat' ); print_r( $data );
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Warning: The magic method Exercise_One\Singleton::__sleep() must have public visibility in /in/m35ma on line 60 Warning: The magic method Exercise_One\Singleton::__wakeup() must have public visibility in /in/m35ma on line 65 Fatal error: Uncaught TypeError: strnatcmp(): Argument #1 ($string1) must be of type string, array given in /in/m35ma:36 Stack trace: #0 /in/m35ma(36): strnatcmp(Array, Array) #1 [internal function]: Exercise_One\{closure}(Array, Array) #2 /in/m35ma(42): uasort(Array, Object(Closure)) #3 /in/m35ma(78): Exercise_One\Singleton::sortBy(Array, 'number', 'ASC', 'cat') #4 {main} thrown in /in/m35ma on line 36
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 Warning: The magic method Exercise_One\Singleton::__sleep() must have public visibility in /in/m35ma on line 60 Warning: The magic method Exercise_One\Singleton::__wakeup() must have public visibility in /in/m35ma on line 65 Fatal error: Uncaught TypeError: strnatcmp(): Argument #1 ($string1) must be of type string, array given in /in/m35ma:36 Stack trace: #0 /in/m35ma(36): strnatcmp(Array, Array) #1 [internal function]: Exercise_One\{closure}(Array, Array) #2 /in/m35ma(42): uasort(Array, Object(Closure)) #3 /in/m35ma(78): Exercise_One\Singleton::sortBy(Array, 'number', 'ASC', 'cat') #4 {main} thrown in /in/m35ma on line 36
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Warning: strnatcmp() expects parameter 1 to be string, array given in /in/m35ma on line 36 Notice: Trying to access array offset on value of type null in /in/m35ma on line 36 Warning: strnatcmp() expects parameter 1 to be string, array given in /in/m35ma on line 36 Notice: Trying to access array offset on value of type null in /in/m35ma on line 36 Warning: strnatcmp() expects parameter 1 to be string, array given in /in/m35ma on line 36 Notice: Trying to access array offset on value of type null in /in/m35ma on line 36 Warning: strnatcmp() expects parameter 1 to be string, array given in /in/m35ma on line 36 Notice: Trying to access array offset on value of type null in /in/m35ma on line 36 Warning: strnatcmp() expects parameter 1 to be string, array given in /in/m35ma on line 36 Notice: Trying to access array offset on value of type null in /in/m35ma on line 36 Array ( [0] => Array ( [id] => 1 [number] => 1 [street] => Battery St [unit] => 1 [rent] => 1200 ) [1] => Array ( [id] => 10 [number] => 10 [street] => Battery St [unit] => 3 [rent] => 1800 ) [2] => Array ( [id] => 2 [number] => 2 [street] => Leavenworth St [unit] => 11 [rent] => 800 ) [3] => Array ( [id] => 12 [number] => 12 [street] => Battery St [unit] => 10 [rent] => 3400 ) [4] => Array ( [id] => 1 [number] => 14 [street] => Leavenworth St [unit] => 10 [rent] => 1450 ) [5] => Array ( [id] => 1 [number] => 01 [street] => Battery St [unit] => 5 [rent] => 1000 ) )
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33
Warning: strnatcmp() expects parameter 1 to be string, array given in /in/m35ma on line 36 Warning: strnatcmp() expects parameter 1 to be string, array given in /in/m35ma on line 36 Warning: strnatcmp() expects parameter 1 to be string, array given in /in/m35ma on line 36 Warning: strnatcmp() expects parameter 1 to be string, array given in /in/m35ma on line 36 Warning: strnatcmp() expects parameter 1 to be string, array given in /in/m35ma on line 36 Array ( [0] => Array ( [id] => 1 [number] => 1 [street] => Battery St [unit] => 1 [rent] => 1200 ) [1] => Array ( [id] => 10 [number] => 10 [street] => Battery St [unit] => 3 [rent] => 1800 ) [2] => Array ( [id] => 2 [number] => 2 [street] => Leavenworth St [unit] => 11 [rent] => 800 ) [3] => Array ( [id] => 12 [number] => 12 [street] => Battery St [unit] => 10 [rent] => 3400 ) [4] => Array ( [id] => 1 [number] => 14 [street] => Leavenworth St [unit] => 10 [rent] => 1450 ) [5] => Array ( [id] => 1 [number] => 01 [street] => Battery St [unit] => 5 [rent] => 1000 ) )
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.30
Catchable fatal error: Argument 3 passed to Exercise_One\sortLogic() must be an instance of Exercise_One\bool, string given, called in /in/m35ma on line 42 and defined in /in/m35ma on line 33
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/m35ma on line 36
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/m35ma on line 2
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/m35ma on line 2
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/m35ma on line 2
Process exited with code 255.

preferences:
332.73 ms | 401 KiB | 412 Q