3v4l.org

run code in 300+ PHP versions simultaneously
<?php $orderList = [ 'Jake', 'Scully' => [ 'pet', 'friend' => [ 'Michael', 'Merissa', ], 'Norm', ], 'Amy', 'Charles', 'Hitchcock', ]; $items = [ [ 'name' => 'Scully', 'related' => [ [ 'name' => 'Norm', 'category' => 'self' ], [ 'name' => 'Earl', 'category' => 'friend' ], [ 'name' => 'Kelly', 'category' => 'pet' ], [ 'name' => 'Michael', 'category' => 'friend' ], [ 'name' => 'Merissa', 'category' => 'friend' ], ], ], [ 'name' => 'Rosa', 'related' => [], ], [ 'name' => 'Jake', 'related' => [ [ 'name' => 'Franzia', 'category' => 'nemesis' ], [ 'name' => 'Doug' ], ], ], [ 'name' => 'Hitchcock', 'related' => [], ], [ 'name' => 'Amy', 'related' => [], ], ]; 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; }; // First, sort parents $items = sort_by($items, $sortKeyResolver, SORT_NATURAL); // Then sort children foreach ($items as &$item) { $children = $item['related'] ?? []; if(empty($orderList[$item['name']])) { // No order was specified for the children, so just sort them as normal $item['related'] = sort_by($children, fn ($child) => $child['name'], SORT_NATURAL); continue; } // An order was specified; it can contain children names or categories, // so let'sjust call it the "Level 2" order (L2). $l2Order = getTopLevelItemsFromMixedList($orderList[$item['name']]); $sortKeyResolver = function ($childItem) use ($l2Order) { // First, we check if there's an entry for the item's name $indexOfChildInL2Order = array_search($childItem['name'], $l2Order); if ($indexOfChildInL2Order !== false) { return $indexOfChildInL2Order; } // Check if there's an entry for the item's category $indexOfCategoryInL2Order = array_search($childItem['category'], $l2Order); if ($indexOfCategoryInL2Order !== false) { // There's an entry for the category! // Now check if there's an entry for the item within the category. $orderOfChildrenInCategory = $l2Order[$childItem['category']] ?? []; $indexOfChildInCategory = array_search($childItem['name'], $orderOfChildrenInCategory); return $indexOfChildInCategory === false ? $indexOfCategoryInL2Order.$childItem['name'] : ($indexOfCategoryInL2Order + ($indexOfChildInCategory * 0.1)); } return count($l2Order).$childItem['name']; }; $item['related'] = sort_by($children, $sortKeyResolver, SORT_NATURAL); } print_r($items);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 16, Position 2 = 50
Branch analysis from position: 16
2 jumps found. (Code = 126) Position 1 = 17, Position 2 = 50
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 33
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 50
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 50
filename:       /in/NDLDT
function name:  (null)
number of ops:  55
compiled vars:  !0 = $orderList, !1 = $items, !2 = $topLevelOrder, !3 = $sortKeyResolver, !4 = $item, !5 = $children, !6 = $l2Order
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   ASSIGN                                                   !0, <array>
   20     1        ASSIGN                                                   !1, <array>
   73     2        INIT_FCALL                                               'gettoplevelitemsfrommixedlist'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $9      
          5        ASSIGN                                                   !2, $9
   74     6        DECLARE_LAMBDA_FUNCTION                          ~11     [0]
          7        BIND_LEXICAL                                             ~11, !2
          8        ASSIGN                                                   !3, ~11
   81     9        INIT_FCALL                                               'sort_by'
         10        SEND_VAR                                                 !1
         11        SEND_VAR                                                 !3
         12        SEND_VAL                                                 6
         13        DO_FCALL                                      0  $13     
         14        ASSIGN                                                   !1, $13
   85    15      > FE_RESET_RW                                      $15     !1, ->50
         16    > > FE_FETCH_RW                                              $15, !4, ->50
   86    17    >   FETCH_DIM_IS                                     ~16     !4, 'related'
         18        COALESCE                                         ~17     ~16
         19        QM_ASSIGN                                        ~17     <array>
         20        ASSIGN                                                   !5, ~17
   88    21        FETCH_DIM_R                                      ~19     !4, 'name'
         22        ISSET_ISEMPTY_DIM_OBJ                         1          !0, ~19
         23      > JMPZ                                                     ~20, ->33
   90    24    >   INIT_FCALL                                               'sort_by'
         25        SEND_VAR                                                 !5
         26        DECLARE_LAMBDA_FUNCTION                          ~22     [1]
         27        SEND_VAL                                                 ~22
         28        SEND_VAL                                                 6
         29        DO_FCALL                                      0  $23     
         30        ASSIGN_DIM                                               !4, 'related'
         31        OP_DATA                                                  $23
   91    32      > JMP                                                      ->16
   96    33    >   INIT_FCALL                                               'gettoplevelitemsfrommixedlist'
         34        FETCH_DIM_R                                      ~24     !4, 'name'
         35        FETCH_DIM_R                                      ~25     !0, ~24
         36        SEND_VAL                                                 ~25
         37        DO_FCALL                                      0  $26     
         38        ASSIGN                                                   !6, $26
   97    39        DECLARE_LAMBDA_FUNCTION                          ~28     [2]
         40        BIND_LEXICAL                                             ~28, !6
         41        ASSIGN                                                   !3, ~28
  120    42        INIT_FCALL                                               'sort_by'
         43        SEND_VAR                                                 !5
         44        SEND_VAR                                                 !3
         45        SEND_VAL                                                 6
         46        DO_FCALL                                      0  $31     
         47        ASSIGN_DIM                                               !4, 'related'
         48        OP_DATA                                                  $31
   85    49      > JMP                                                      ->16
         50    >   FE_FREE                                                  $15
  123    51        INIT_FCALL                                               'print_r'
         52        SEND_VAR                                                 !1
         53        DO_ICALL                                                 
         54      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
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/NDLDT
function name:  {closure}
number of ops:  18
compiled vars:  !0 = $item, !1 = $topLevelOrder, !2 = $orderListIndex
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   75     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
   76     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
   77    17*     > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/NDLDT
function name:  {closure}
number of ops:  4
compiled vars:  !0 = $child
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   90     0  E >   RECV                                             !0      
          1        FETCH_DIM_R                                      ~1      !0, 'name'
          2      > RETURN                                                   ~1
          3*     > RETURN                                                   null

End of Dynamic Function 1

Dynamic Function 2
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 11
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 40
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 36
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/NDLDT
function name:  {closure}
number of ops:  45
compiled vars:  !0 = $childItem, !1 = $l2Order, !2 = $indexOfChildInL2Order, !3 = $indexOfCategoryInL2Order, !4 = $orderOfChildrenInCategory, !5 = $indexOfChildInCategory
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   97     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   99     2        INIT_FCALL                                               'array_search'
          3        FETCH_DIM_R                                      ~6      !0, 'name'
          4        SEND_VAL                                                 ~6
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $7      
          7        ASSIGN                                                   !2, $7
  100     8        TYPE_CHECK                                  1018          !2
          9      > JMPZ                                                     ~9, ->11
  101    10    > > RETURN                                                   !2
  105    11    >   INIT_FCALL                                               'array_search'
         12        FETCH_DIM_R                                      ~10     !0, 'category'
         13        SEND_VAL                                                 ~10
         14        SEND_VAR                                                 !1
         15        DO_ICALL                                         $11     
         16        ASSIGN                                                   !3, $11
  106    17        TYPE_CHECK                                  1018          !3
         18      > JMPZ                                                     ~13, ->40
  109    19    >   FETCH_DIM_R                                      ~14     !0, 'category'
         20        FETCH_DIM_IS                                     ~15     !1, ~14
         21        COALESCE                                         ~16     ~15
         22        QM_ASSIGN                                        ~16     <array>
         23        ASSIGN                                                   !4, ~16
  110    24        INIT_FCALL                                               'array_search'
         25        FETCH_DIM_R                                      ~18     !0, 'name'
         26        SEND_VAL                                                 ~18
         27        SEND_VAR                                                 !4
         28        DO_ICALL                                         $19     
         29        ASSIGN                                                   !5, $19
  112    30        TYPE_CHECK                                    4          !5
         31      > JMPZ                                                     ~21, ->36
  113    32    >   FETCH_DIM_R                                      ~22     !0, 'name'
         33        CONCAT                                           ~23     !3, ~22
         34        QM_ASSIGN                                        ~24     ~23
         35      > JMP                                                      ->39
  114    36    >   MUL                                              ~25     !5, 0.1
         37        ADD                                              ~26     !3, ~25
         38        QM_ASSIGN                                        ~24     ~26
         39    > > RETURN                                                   ~24
  117    40    >   COUNT                                            ~27     !1
         41        FETCH_DIM_R                                      ~28     !0, 'name'
         42        CONCAT                                           ~29     ~27, ~28
         43      > RETURN                                                   ~29
  118    44*     > RETURN                                                   null

End of Dynamic Function 2

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/NDLDT
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
-------------------------------------------------------------------------------------
   53     0  E >   RECV                                             !0      
   54     1        ASSIGN                                                   !1, <array>
   55     2      > FE_RESET_R                                       $5      !0, ->13
          3    > > FE_FETCH_R                                       ~6      $5, !2, ->13
          4    >   ASSIGN                                                   !3, ~6
   56     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
   55    12      > JMP                                                      ->3
         13    >   FE_FREE                                                  $5
   58    14        VERIFY_RETURN_TYPE                                       !1
         15      > RETURN                                                   !1
   59    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/NDLDT
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
-------------------------------------------------------------------------------------
   61     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_VARIADIC                                    !2      
   62     3        INIT_FCALL                                               'array_map'
          4        SEND_VAR                                                 !1
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $7      
          7        ASSIGN                                                   !3, $7
   63     8        INIT_FCALL                                               'asort'
          9        SEND_REF                                                 !3
         10        SEND_UNPACK                                              !2
         11        CHECK_UNDEF_ARGS                                         
         12        DO_ICALL                                                 
   65    13        ASSIGN                                                   !4, <array>
   66    14      > FE_RESET_R                                       $11     !3, ->21
         15    > > FE_FETCH_R                                       ~12     $11, !5, ->21
         16    >   ASSIGN                                                   !6, ~12
   67    17        FETCH_DIM_R                                      ~15     !0, !6
         18        ASSIGN_DIM                                               !4
         19        OP_DATA                                                  ~15
   66    20      > JMP                                                      ->15
         21    >   FE_FREE                                                  $11
   70    22      > RETURN                                                   !4
   71    23*     > RETURN                                                   null

End of function sort_by

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.49 ms | 1482 KiB | 22 Q