3v4l.org

run code in 300+ PHP versions simultaneously
<?php // array of characters depending on other characters $characters = array( 'a' => array('c'), 'b' => array('c', 'a'), 'd' => array('c', 'a'), 'c' => array(), 'l' => array('c'), 'f' => array('l'), 't' => array('a'), 'w' => array('c', 'a'), ); function sortCharacters($characters) { uksort ($characters, function ($a, $b) use ($characters) { if (empty($characters[$a]) && empty($characters[$b])) { // equal (no dependencies) return 0; } if (empty($characters[$a]) && !empty($characters[$b])) { // a has no dependencies so is lower return -1; } if (!empty($characters[$a]) && empty($characters[$b])) { // b has no dependencies so a is higher return 1; } if (in_array($a, $characters[$b])) { // b depends on a so a is lower return -1; } if (in_array($b, $characters[$a])) { // a depends on b so a is higher return 1; } return 0; }); return $characters; } $characters = sortCharacters($characters); print_r($characters);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Lfq4C
function name:  (null)
number of ops:  9
compiled vars:  !0 = $characters
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   ASSIGN                                                   !0, <array>
   47     1        INIT_FCALL                                               'sortcharacters'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $2      
          4        ASSIGN                                                   !0, $2
   48     5        INIT_FCALL                                               'print_r'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                                 
          8      > RETURN                                                   1

Function sortcharacters:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Lfq4C
function name:  sortCharacters
number of ops:  9
compiled vars:  !0 = $characters
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
   17     1        INIT_FCALL                                               'uksort'
          2        SEND_REF                                                 !0
          3        DECLARE_LAMBDA_FUNCTION                          ~1      [0]
          4        BIND_LEXICAL                                             ~1, !0
   42     5        SEND_VAL                                                 ~1
   17     6        DO_ICALL                                                 
   44     7      > RETURN                                                   !0
   45     8*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 5, Position 2 = 7
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 9
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 46) Position 1 = 11, Position 2 = 14
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 16
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
2 jumps found. (Code = 46) Position 1 = 19, Position 2 = 21
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 23
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 30
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 37
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
Branch analysis from position: 14
Branch analysis from position: 7
filename:       /in/Lfq4C
function name:  {closure}
number of ops:  39
compiled vars:  !0 = $a, !1 = $b, !2 = $characters
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
   18     3        ISSET_ISEMPTY_DIM_OBJ                         1  ~3      !2, !0
          4      > JMPZ_EX                                          ~3      ~3, ->7
          5    >   ISSET_ISEMPTY_DIM_OBJ                         1  ~4      !2, !1
          6        BOOL                                             ~3      ~4
          7    > > JMPZ                                                     ~3, ->9
   20     8    > > RETURN                                                   0
   22     9    >   ISSET_ISEMPTY_DIM_OBJ                         1  ~5      !2, !0
         10      > JMPZ_EX                                          ~5      ~5, ->14
         11    >   ISSET_ISEMPTY_DIM_OBJ                         1  ~6      !2, !1
         12        BOOL_NOT                                         ~7      ~6
         13        BOOL                                             ~5      ~7
         14    > > JMPZ                                                     ~5, ->16
   24    15    > > RETURN                                                   -1
   26    16    >   ISSET_ISEMPTY_DIM_OBJ                         1  ~8      !2, !0
         17        BOOL_NOT                                         ~9      ~8
         18      > JMPZ_EX                                          ~9      ~9, ->21
         19    >   ISSET_ISEMPTY_DIM_OBJ                         1  ~10     !2, !1
         20        BOOL                                             ~9      ~10
         21    > > JMPZ                                                     ~9, ->23
   28    22    > > RETURN                                                   1
   31    23    >   INIT_FCALL                                               'in_array'
         24        SEND_VAR                                                 !0
         25        FETCH_DIM_R                                      ~11     !2, !1
         26        SEND_VAL                                                 ~11
         27        DO_ICALL                                         $12     
         28      > JMPZ                                                     $12, ->30
   33    29    > > RETURN                                                   -1
   36    30    >   INIT_FCALL                                               'in_array'
         31        SEND_VAR                                                 !1
         32        FETCH_DIM_R                                      ~13     !2, !0
         33        SEND_VAL                                                 ~13
         34        DO_ICALL                                         $14     
         35      > JMPZ                                                     $14, ->37
   38    36    > > RETURN                                                   1
   41    37    > > RETURN                                                   0
   42    38*     > RETURN                                                   null

End of Dynamic Function 0

End of function sortcharacters

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.04 ms | 1007 KiB | 17 Q