3v4l.org

run code in 300+ PHP versions simultaneously
<?php $orderList = [ 'Jake', 'Scully' => [ 'pet', 'friend' => [ 'Michael', 'Merissa', ], 'Norm', ], 'Amy', 'Charles', 'Hitchcock', ]; $items = [ [ 'name' => 'Scully' ], [ 'name' => 'Rosa' ], [ 'name' => 'Bill' ], [ 'name' => 'Jake' ], [ 'name' => 'Hitchcock' ], [ 'name' => 'Amy' ], ]; function getTopLevelItemsFromMixedList(array $mixedList): array { $topLevels = []; foreach ($mixedList as $item => $value) { $topLevels[] = is_int($item) ? $value : $item; } return $topLevels; } function sort_by(array $array, callable $sortKeyResolver, ...$sortArgs) { $mapped = array_map($sortKeyResolver, $array); asort($mapped, ...$sortArgs); $sorted = []; foreach ($mapped as $key => $index) { $sorted[] = $array[$key]; } return $sorted; } $topLevelOrder = getTopLevelItemsFromMixedList($orderList); $sortKeyResolver = function ($item) use ($topLevelOrder) { $orderListIndex = array_search($item['name'], $topLevelOrder); return $orderListIndex === false ? count($topLevelOrder).$item['name'] : $orderListIndex; }; print_r(sort_by($items, $sortKeyResolver, SORT_NATURAL));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OAhUZ
function name:  (null)
number of ops:  18
compiled vars:  !0 = $orderList, !1 = $items, !2 = $topLevelOrder, !3 = $sortKeyResolver
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   18     1        ASSIGN                                                   !1, <array>
   48     2        INIT_FCALL                                               'gettoplevelitemsfrommixedlist'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $6      
          5        ASSIGN                                                   !2, $6
   49     6        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FOAhUZ%3A49%240'
          7        BIND_LEXICAL                                             ~8, !2
          8        ASSIGN                                                   !3, ~8
   54     9        INIT_FCALL                                               'print_r'
         10        INIT_FCALL                                               'sort_by'
         11        SEND_VAR                                                 !1
         12        SEND_VAR                                                 !3
         13        SEND_VAL                                                 6
         14        DO_FCALL                                      0  $10     
         15        SEND_VAR                                                 $10
         16        DO_ICALL                                                 
         17      > RETURN                                                   1

Function gettoplevelitemsfrommixedlist:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 13
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 13
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/OAhUZ
function name:  getTopLevelItemsFromMixedList
number of ops:  18
compiled vars:  !0 = $mixedList, !1 = $topLevels, !2 = $value, !3 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
   29     1        ASSIGN                                                   !1, <array>
   30     2      > FE_RESET_R                                       $5      !0, ->13
          3    > > FE_FETCH_R                                       ~6      $5, !2, ->13
          4    >   ASSIGN                                                   !3, ~6
   31     5        TYPE_CHECK                                   16          !3
          6      > JMPZ                                                     ~9, ->9
          7    >   QM_ASSIGN                                        ~10     !2
          8      > JMP                                                      ->10
          9    >   QM_ASSIGN                                        ~10     !3
         10    >   ASSIGN_DIM                                               !1
         11        OP_DATA                                                  ~10
   30    12      > JMP                                                      ->3
         13    >   FE_FREE                                                  $5
   33    14        VERIFY_RETURN_TYPE                                       !1
         15      > RETURN                                                   !1
   34    16*       VERIFY_RETURN_TYPE                                       
         17*     > RETURN                                                   null

End of function gettoplevelitemsfrommixedlist

Function sort_by:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 21
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 21
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
filename:       /in/OAhUZ
function name:  sort_by
number of ops:  24
compiled vars:  !0 = $array, !1 = $sortKeyResolver, !2 = $sortArgs, !3 = $mapped, !4 = $sorted, !5 = $index, !6 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_VARIADIC                                    !2      
   37     3        INIT_FCALL                                               'array_map'
          4        SEND_VAR                                                 !1
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $7      
          7        ASSIGN                                                   !3, $7
   38     8        INIT_FCALL                                               'asort'
          9        SEND_REF                                                 !3
         10        SEND_UNPACK                                              !2
         11        CHECK_UNDEF_ARGS                                         
         12        DO_ICALL                                                 
   40    13        ASSIGN                                                   !4, <array>
   41    14      > FE_RESET_R                                       $11     !3, ->21
         15    > > FE_FETCH_R                                       ~12     $11, !5, ->21
         16    >   ASSIGN                                                   !6, ~12
   42    17        FETCH_DIM_R                                      ~15     !0, !6
         18        ASSIGN_DIM                                               !4
         19        OP_DATA                                                  ~15
   41    20      > JMP                                                      ->15
         21    >   FE_FREE                                                  $11
   45    22      > RETURN                                                   !4
   46    23*     > RETURN                                                   null

End of function sort_by

Function %00%7Bclosure%7D%2Fin%2FOAhUZ%3A49%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 15
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OAhUZ
function name:  {closure}
number of ops:  18
compiled vars:  !0 = $item, !1 = $topLevelOrder, !2 = $orderListIndex
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   50     2        INIT_FCALL                                               'array_search'
          3        FETCH_DIM_R                                      ~3      !0, 'name'
          4        SEND_VAL                                                 ~3
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $4      
          7        ASSIGN                                                   !2, $4
   51     8        TYPE_CHECK                                    4          !2
          9      > JMPZ                                                     ~6, ->15
         10    >   COUNT                                            ~7      !1
         11        FETCH_DIM_R                                      ~8      !0, 'name'
         12        CONCAT                                           ~9      ~7, ~8
         13        QM_ASSIGN                                        ~10     ~9
         14      > JMP                                                      ->16
         15    >   QM_ASSIGN                                        ~10     !2
         16    > > RETURN                                                   ~10
   52    17*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FOAhUZ%3A49%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
181.59 ms | 1407 KiB | 23 Q