3v4l.org

run code in 300+ PHP versions simultaneously
<?php $a = Array(0 => Array('member_id' => 6, 'total' => 1, 'sorting' => 0), 1 => Array('member_id' => 7, 'total' => 2, 'sorting' => 1) ); $b = Array(0 => Array('member_id' => 2, 'total' => 3), 1 => Array('member_id' => 6, 'total' => 1), 2 => Array('member_id' => 7, 'total' => 2) ); /* * first, store all records with matching member_id */ $c = []; foreach ($b as $key0 => $value0) { foreach ($a as $key1 => $value1) { // matching 'member_id'? if ($value0['member_id'] === $value1['member_id']) { // inject record into store $c $c[] = $a[$key1]; } } } /* * second, inject non-matching records from $b into store $c */ $ids = array_column($c, 'member_id'); foreach ($b as $key => $value) { if (!in_array($value['member_id'], $ids)) { $sorting = max(array_column($c, 'sorting')) + 1; $value['sorting'] = $sorting; $c[] = $value; } } echo '<pre>'; print_r($c); echo '</pre>';
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 19
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 19
Branch analysis from position: 5
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 17
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 17
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 16
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 16
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 17
Branch analysis from position: 19
2 jumps found. (Code = 77) Position 1 = 26, Position 2 = 49
Branch analysis from position: 26
2 jumps found. (Code = 78) Position 1 = 27, Position 2 = 49
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 48
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
Branch analysis from position: 48
Branch analysis from position: 49
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 49
Branch analysis from position: 19
filename:       /in/GkSGn
function name:  (null)
number of ops:  56
compiled vars:  !0 = $a, !1 = $b, !2 = $c, !3 = $value0, !4 = $key0, !5 = $value1, !6 = $key1, !7 = $ids, !8 = $value, !9 = $key, !10 = $sorting
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
    7     1        ASSIGN                                                   !1, <array>
   14     2        ASSIGN                                                   !2, <array>
   15     3      > FE_RESET_R                                       $14     !1, ->19
          4    > > FE_FETCH_R                                       ~15     $14, !3, ->19
          5    >   ASSIGN                                                   !4, ~15
   16     6      > FE_RESET_R                                       $17     !0, ->17
          7    > > FE_FETCH_R                                       ~18     $17, !5, ->17
          8    >   ASSIGN                                                   !6, ~18
   18     9        FETCH_DIM_R                                      ~20     !3, 'member_id'
         10        FETCH_DIM_R                                      ~21     !5, 'member_id'
         11        IS_IDENTICAL                                             ~20, ~21
         12      > JMPZ                                                     ~22, ->16
   20    13    >   FETCH_DIM_R                                      ~24     !0, !6
         14        ASSIGN_DIM                                               !2
         15        OP_DATA                                                  ~24
   16    16    > > JMP                                                      ->7
         17    >   FE_FREE                                                  $17
   15    18      > JMP                                                      ->4
         19    >   FE_FREE                                                  $14
   28    20        INIT_FCALL                                               'array_column'
         21        SEND_VAR                                                 !2
         22        SEND_VAL                                                 'member_id'
         23        DO_ICALL                                         $25     
         24        ASSIGN                                                   !7, $25
   29    25      > FE_RESET_R                                       $27     !1, ->49
         26    > > FE_FETCH_R                                       ~28     $27, !8, ->49
         27    >   ASSIGN                                                   !9, ~28
   30    28        INIT_FCALL                                               'in_array'
         29        FETCH_DIM_R                                      ~30     !8, 'member_id'
         30        SEND_VAL                                                 ~30
         31        SEND_VAR                                                 !7
         32        DO_ICALL                                         $31     
         33        BOOL_NOT                                         ~32     $31
         34      > JMPZ                                                     ~32, ->48
   31    35    >   INIT_FCALL                                               'max'
         36        INIT_FCALL                                               'array_column'
         37        SEND_VAR                                                 !2
         38        SEND_VAL                                                 'sorting'
         39        DO_ICALL                                         $33     
         40        SEND_VAR                                                 $33
         41        DO_ICALL                                         $34     
         42        ADD                                              ~35     $34, 1
         43        ASSIGN                                                   !10, ~35
   32    44        ASSIGN_DIM                                               !8, 'sorting'
         45        OP_DATA                                                  !10
   33    46        ASSIGN_DIM                                               !2
         47        OP_DATA                                                  !8
   29    48    > > JMP                                                      ->26
         49    >   FE_FREE                                                  $27
   38    50        ECHO                                                     '%3Cpre%3E'
   39    51        INIT_FCALL                                               'print_r'
         52        SEND_VAR                                                 !2
         53        DO_ICALL                                                 
   40    54        ECHO                                                     '%3C%2Fpre%3E'
         55      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.87 ms | 960 KiB | 17 Q