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 getComparator(array $order): callable { return function ($a, $b) use ($order) { $indexOfA = array_search($a['name'], $order); $indexOfB = array_search($b['name'], $order); // If both are in the $order list, compare them based on their position in the list if ($indexOfA !== false && $indexOfB !== false) { return $indexOfA <=> $indexOfB; } // If only A is in the $order list, then it must come before B. if ($indexOfA !== false) { return -1; } // If only B is in the $order list, then it must come before A. if ($indexOfB !== false) { return 1; } // If neither is present, fall back to natural sort return strnatcmp($a['name'], $b['name']); }; } $topLevelOrder = getTopLevelItemsFromMixedList($orderList); usort($items, getComparator($topLevelOrder)); print_r($items);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/t62Gm
function name:  (null)
number of ops:  17
compiled vars:  !0 = $orderList, !1 = $items, !2 = $topLevelOrder
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   ASSIGN                                                   !0, <array>
   19     1        ASSIGN                                                   !1, <array>
   74     2        INIT_FCALL                                               'gettoplevelitemsfrommixedlist'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $5      
          5        ASSIGN                                                   !2, $5
   75     6        INIT_FCALL                                               'usort'
          7        SEND_REF                                                 !1
          8        INIT_FCALL                                               'getcomparator'
          9        SEND_VAR                                                 !2
         10        DO_FCALL                                      0  $7      
         11        SEND_VAR                                                 $7
         12        DO_ICALL                                                 
   77    13        INIT_FCALL                                               'print_r'
         14        SEND_VAR                                                 !1
         15        DO_ICALL                                                 
         16      > 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/t62Gm
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
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
   42     1        ASSIGN                                                   !1, <array>
   43     2      > FE_RESET_R                                       $5      !0, ->13
          3    > > FE_FETCH_R                                       ~6      $5, !2, ->13
          4    >   ASSIGN                                                   !3, ~6
   44     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
   43    12      > JMP                                                      ->3
         13    >   FE_FREE                                                  $5
   46    14        VERIFY_RETURN_TYPE                                       !1
         15      > RETURN                                                   !1
   47    16*       VERIFY_RETURN_TYPE                                       
         17*     > RETURN                                                   null

End of function gettoplevelitemsfrommixedlist

Function getcomparator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/t62Gm
function name:  getComparator
number of ops:  7
compiled vars:  !0 = $order
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   RECV                                             !0      
   50     1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Ft62Gm%3A50%240'
          2        BIND_LEXICAL                                             ~1, !0
   71     3        VERIFY_RETURN_TYPE                                       ~1
          4      > RETURN                                                   ~1
   72     5*       VERIFY_RETURN_TYPE                                       
          6*     > RETURN                                                   null

End of function getcomparator

Function %00%7Bclosure%7D%2Fin%2Ft62Gm%3A50%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 17, Position 2 = 19
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 22
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 25
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 28
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/t62Gm
function name:  {closure}
number of ops:  36
compiled vars:  !0 = $a, !1 = $b, !2 = $order, !3 = $indexOfA, !4 = $indexOfB
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
   51     3        INIT_FCALL                                               'array_search'
          4        FETCH_DIM_R                                      ~5      !0, 'name'
          5        SEND_VAL                                                 ~5
          6        SEND_VAR                                                 !2
          7        DO_ICALL                                         $6      
          8        ASSIGN                                                   !3, $6
   52     9        INIT_FCALL                                               'array_search'
         10        FETCH_DIM_R                                      ~8      !1, 'name'
         11        SEND_VAL                                                 ~8
         12        SEND_VAR                                                 !2
         13        DO_ICALL                                         $9      
         14        ASSIGN                                                   !4, $9
   55    15        TYPE_CHECK                                  1018  ~11     !3
         16      > JMPZ_EX                                          ~11     ~11, ->19
         17    >   TYPE_CHECK                                  1018  ~12     !4
         18        BOOL                                             ~11     ~12
         19    > > JMPZ                                                     ~11, ->22
   56    20    >   SPACESHIP                                        ~13     !3, !4
         21      > RETURN                                                   ~13
   60    22    >   TYPE_CHECK                                  1018          !3
         23      > JMPZ                                                     ~14, ->25
   61    24    > > RETURN                                                   -1
   65    25    >   TYPE_CHECK                                  1018          !4
         26      > JMPZ                                                     ~15, ->28
   66    27    > > RETURN                                                   1
   70    28    >   INIT_FCALL                                               'strnatcmp'
         29        FETCH_DIM_R                                      ~16     !0, 'name'
         30        SEND_VAL                                                 ~16
         31        FETCH_DIM_R                                      ~17     !1, 'name'
         32        SEND_VAL                                                 ~17
         33        DO_ICALL                                         $18     
         34      > RETURN                                                   $18
   71    35*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Ft62Gm%3A50%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.55 ms | 1407 KiB | 23 Q