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[$key], $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 );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iF8dF
function name:  (null)
number of ops:  23
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     0  E >   ASSIGN                                                   !0, <array>
   71     1        ASSIGN_DIM                                               !0
          2        OP_DATA                                                  <array>
   72     3        ASSIGN_DIM                                               !0
          4        OP_DATA                                                  <array>
   73     5        ASSIGN_DIM                                               !0
          6        OP_DATA                                                  <array>
   74     7        ASSIGN_DIM                                               !0
          8        OP_DATA                                                  <array>
   75     9        ASSIGN_DIM                                               !0
         10        OP_DATA                                                  <array>
   76    11        ASSIGN_DIM                                               !0
         12        OP_DATA                                                  <array>
   78    13        INIT_STATIC_METHOD_CALL                                  'Exercise_One%5CSingleton', 'sortBy'
         14        SEND_REF                                                 !0
         15        SEND_VAL                                                 'number'
         16        SEND_VAL                                                 'ASC'
         17        SEND_VAL                                                 'cat'
         18        DO_FCALL                                      0          
   79    19        INIT_NS_FCALL_BY_NAME                                    'Exercise_One%5Cprint_r'
         20        SEND_VAR_EX                                              !0
         21        DO_FCALL                                      0          
         22      > RETURN                                                   1

Function %00exercise_one%5Csortlogic%2Fin%2FiF8dF%3A33%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iF8dF
function name:  Exercise_One\sortLogic
number of ops:  9
compiled vars:  !0 = $key, !1 = $dir, !2 = $nat
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   34     3        DECLARE_LAMBDA_FUNCTION                                  '%00exercise_one%5C%7Bclosure%7D%2Fin%2FiF8dF%3A34%241'
          4        BIND_LEXICAL                                             ~3, !0
          5        BIND_LEXICAL                                             ~3, !1
          6        BIND_LEXICAL                                             ~3, !2
   40     7      > RETURN                                                   ~3
   41     8*     > RETURN                                                   null

End of function %00exercise_one%5Csortlogic%2Fin%2FiF8dF%3A33%240

Function %00exercise_one%5C%7Bclosure%7D%2Fin%2FiF8dF%3A34%241:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 17
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 24
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iF8dF
function name:  Exercise_One\{closure}
number of ops:  30
compiled vars:  !0 = $a, !1 = $b, !2 = $key, !3 = $dir, !4 = $nat
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
          3        BIND_STATIC                                              !3
          4        BIND_STATIC                                              !4
   35     5        TYPE_CHECK                                    8          !4
          6      > JMPZ                                                     ~5, ->17
   36     7    >   INIT_NS_FCALL_BY_NAME                                    'Exercise_One%5Cstrnatcmp'
          8        CHECK_FUNC_ARG                                           
          9        FETCH_DIM_FUNC_ARG                               $6      !0, !2
         10        SEND_FUNC_ARG                                            $6
         11        CHECK_FUNC_ARG                                           
         12        FETCH_DIM_FUNC_ARG                               $7      !1, !2
         13        SEND_FUNC_ARG                                            $7
         14        DO_FCALL                                      0  $8      
         15      > RETURN                                                   $8
         16*       JMP                                                      ->29
   38    17    >   IS_IDENTICAL                                             !3, 'ASC'
         18      > JMPZ                                                     ~9, ->24
         19    >   FETCH_DIM_R                                      ~10     !0, !2
         20        FETCH_DIM_R                                      ~11     !1, !2
         21        IS_SMALLER                                       ~12     ~11, ~10
         22        QM_ASSIGN                                        ~13     ~12
         23      > JMP                                                      ->28
         24    >   FETCH_DIM_R                                      ~14     !0, !2
         25        FETCH_DIM_R                                      ~15     !1, !2
         26        IS_SMALLER                                       ~16     ~14, ~15
         27        QM_ASSIGN                                        ~13     ~16
         28    > > RETURN                                                   ~13
   40    29*     > RETURN                                                   null

End of function %00exercise_one%5C%7Bclosure%7D%2Fin%2FiF8dF%3A34%241

Class Exercise_One\Singleton:
Function sortby:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 11
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/iF8dF
function name:  sortBy
number of ops:  24
compiled vars:  !0 = $array, !1 = $sortKey, !2 = $dir, !3 = $nat
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      'ASC'
          3        RECV_INIT                                        !3      <false>
   29     4        FETCH_STATIC_PROP_R          unknown             ~4      'instance'
          5        TYPE_CHECK                                    2          ~4
          6      > JMPZ                                                     ~5, ->11
   30     7    >   NEW                          static              $7      
          8        DO_FCALL                                      0          
          9        ASSIGN_STATIC_PROP                                       'instance'
         10        OP_DATA                                                  $7
   33    11    >   DECLARE_FUNCTION                                         'exercise_one%5Csortlogic'
   42    12        INIT_NS_FCALL_BY_NAME                                    'Exercise_One%5Cuasort'
         13        SEND_VAR_EX                                              !0
         14        INIT_NS_FCALL_BY_NAME                                    'Exercise_One%5CsortLogic'
         15        SEND_VAR_EX                                              !1
         16        SEND_VAR_EX                                              !2
         17        SEND_VAR_EX                                              !3
         18        DO_FCALL                                      0  $9      
         19        SEND_VAR_NO_REF_EX                                       $9
         20        DO_FCALL                                      0          
   44    21        FETCH_STATIC_PROP_R          unknown             ~11     'instance'
         22      > RETURN                                                   ~11
   45    23*     > RETURN                                                   null

End of function sortby

Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iF8dF
function name:  __construct
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E > > RETURN                                                   null

End of function __construct

Function __clone:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iF8dF
function name:  __clone
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E > > RETURN                                                   null

End of function __clone

Function __sleep:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iF8dF
function name:  __sleep
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E > > RETURN                                                   null

End of function __sleep

Function __wakeup:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iF8dF
function name:  __wakeup
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E > > RETURN                                                   null

End of function __wakeup

End of class Exercise_One\Singleton.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.37 ms | 1398 KiB | 20 Q