3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ ['page' => '32', 'position' => 'append', 'priority' => '1'], ['page' => '32', 'position' => 'append', 'priority' => '2'], ['page' => '32', 'position' => 'prepend', 'priority' => '3'], ['page' => '32', 'position' => 'both', 'priority' => '3'], ['page' => '32', 'position' => 'prepend', 'priority' => '4'], ['page' => '143', 'position' => 'prepend', 'priority' => '18'], ['page' => '143', 'position' => 'append', 'priority' => '18'], ['page' => '143', 'position' => 'prepend', 'priority' => '19'], ]; $result = []; foreach ($array as $row) { if (!isset($result[$row['page']])) { $result[$row['page']] = ['both' => [], 'non-both' => []]; } if ($row['position'] !== 'both') { if ($result[$row['page']]['both']) { continue; // nothing worth doing in this case, ignore the row } else { $result[$row['page']]['non-both'][] = $row; } } else { $result[$row['page']]['both'][] = $row; } } foreach ($result as $page => $rows) { $keep = []; foreach ($rows['both'] ?: $rows['non-both'] as $row) { if (!$keep || $row['priority'] < $keep[0]['priority']) { $keep = [$row]; } elseif ($row['priority'] === $keep[0]['priority']) { $keep[] = $row; } } if ($keep[0]['position'] !== 'both') { rsort($keep); } $result[$page] = $keep; } var_export($result);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 32
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 32
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 11
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 26
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 20
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 11
Branch analysis from position: 32
2 jumps found. (Code = 77) Position 1 = 34, Position 2 = 73
Branch analysis from position: 34
2 jumps found. (Code = 78) Position 1 = 35, Position 2 = 73
Branch analysis from position: 35
2 jumps found. (Code = 77) Position 1 = 42, Position 2 = 62
Branch analysis from position: 42
2 jumps found. (Code = 78) Position 1 = 43, Position 2 = 62
Branch analysis from position: 43
2 jumps found. (Code = 47) Position 1 = 45, Position 2 = 50
Branch analysis from position: 45
2 jumps found. (Code = 43) Position 1 = 51, Position 2 = 54
Branch analysis from position: 51
1 jumps found. (Code = 42) Position 1 = 61
Branch analysis from position: 61
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
Branch analysis from position: 54
2 jumps found. (Code = 43) Position 1 = 59, Position 2 = 61
Branch analysis from position: 59
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
Branch analysis from position: 61
Branch analysis from position: 50
Branch analysis from position: 62
2 jumps found. (Code = 43) Position 1 = 67, Position 2 = 70
Branch analysis from position: 67
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
Branch analysis from position: 70
Branch analysis from position: 62
Branch analysis from position: 73
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 73
Branch analysis from position: 32
filename:       /in/IuNCF
function name:  (null)
number of ops:  78
compiled vars:  !0 = $array, !1 = $result, !2 = $row, !3 = $rows, !4 = $page, !5 = $keep
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   14     1        ASSIGN                                                   !1, <array>
   15     2      > FE_RESET_R                                       $8      !0, ->32
          3    > > FE_FETCH_R                                               $8, !2, ->32
   16     4    >   FETCH_DIM_R                                      ~9      !2, 'page'
          5        ISSET_ISEMPTY_DIM_OBJ                         0  ~10     !1, ~9
          6        BOOL_NOT                                         ~11     ~10
          7      > JMPZ                                                     ~11, ->11
   17     8    >   FETCH_DIM_R                                      ~12     !2, 'page'
          9        ASSIGN_DIM                                               !1, ~12
         10        OP_DATA                                                  <array>
   19    11    >   FETCH_DIM_R                                      ~14     !2, 'position'
         12        IS_NOT_IDENTICAL                                         ~14, 'both'
         13      > JMPZ                                                     ~15, ->26
   20    14    >   FETCH_DIM_R                                      ~16     !2, 'page'
         15        FETCH_DIM_R                                      ~17     !1, ~16
         16        FETCH_DIM_R                                      ~18     ~17, 'both'
         17      > JMPZ                                                     ~18, ->20
   21    18    > > JMP                                                      ->3
   20    19*       JMP                                                      ->25
   23    20    >   FETCH_DIM_R                                      ~19     !2, 'page'
         21        FETCH_DIM_W                                      $20     !1, ~19
         22        FETCH_DIM_W                                      $21     $20, 'non-both'
         23        ASSIGN_DIM                                               $21
         24        OP_DATA                                                  !2
   19    25      > JMP                                                      ->31
   26    26    >   FETCH_DIM_R                                      ~23     !2, 'page'
         27        FETCH_DIM_W                                      $24     !1, ~23
         28        FETCH_DIM_W                                      $25     $24, 'both'
         29        ASSIGN_DIM                                               $25
         30        OP_DATA                                                  !2
   15    31    > > JMP                                                      ->3
         32    >   FE_FREE                                                  $8
   30    33      > FE_RESET_R                                       $27     !1, ->73
         34    > > FE_FETCH_R                                       ~28     $27, !3, ->73
         35    >   ASSIGN                                                   !4, ~28
   31    36        ASSIGN                                                   !5, <array>
   32    37        FETCH_DIM_R                                      ~31     !3, 'both'
         38        JMP_SET                                          ~32     ~31, ->41
         39        FETCH_DIM_R                                      ~33     !3, 'non-both'
         40        QM_ASSIGN                                        ~32     ~33
         41      > FE_RESET_R                                       $34     ~32, ->62
         42    > > FE_FETCH_R                                               $34, !2, ->62
   33    43    >   BOOL_NOT                                         ~35     !5
         44      > JMPNZ_EX                                         ~35     ~35, ->50
         45    >   FETCH_DIM_R                                      ~36     !2, 'priority'
         46        FETCH_DIM_R                                      ~37     !5, 0
         47        FETCH_DIM_R                                      ~38     ~37, 'priority'
         48        IS_SMALLER                                       ~39     ~36, ~38
         49        BOOL                                             ~35     ~39
         50    > > JMPZ                                                     ~35, ->54
   34    51    >   INIT_ARRAY                                       ~40     !2
         52        ASSIGN                                                   !5, ~40
   33    53      > JMP                                                      ->61
   35    54    >   FETCH_DIM_R                                      ~42     !2, 'priority'
         55        FETCH_DIM_R                                      ~43     !5, 0
         56        FETCH_DIM_R                                      ~44     ~43, 'priority'
         57        IS_IDENTICAL                                             ~42, ~44
         58      > JMPZ                                                     ~45, ->61
   36    59    >   ASSIGN_DIM                                               !5
         60        OP_DATA                                                  !2
   32    61    > > JMP                                                      ->42
         62    >   FE_FREE                                                  $34
   39    63        FETCH_DIM_R                                      ~47     !5, 0
         64        FETCH_DIM_R                                      ~48     ~47, 'position'
         65        IS_NOT_IDENTICAL                                         ~48, 'both'
         66      > JMPZ                                                     ~49, ->70
   40    67    >   INIT_FCALL                                               'rsort'
         68        SEND_REF                                                 !5
         69        DO_ICALL                                                 
   42    70    >   ASSIGN_DIM                                               !1, !4
         71        OP_DATA                                                  !5
   30    72      > JMP                                                      ->34
         73    >   FE_FREE                                                  $27
   44    74        INIT_FCALL                                               'var_export'
         75        SEND_VAR                                                 !1
         76        DO_ICALL                                                 
         77      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.05 ms | 1020 KiB | 15 Q