3v4l.org

run code in 300+ PHP versions simultaneously
<?php function find_pairs($array) { // re-index to 0 $array = array_values($array['parent_id']); // sort so the longest arrays are first usort($array, function ($a, $b) { return count($b) - count($a); }); // output array $pairs = array(); for ($i = 0, $j = 1; $i < count($array) - 1; $i++) { if (!count($array[$i])) continue; while ($j <= $i || $j < count($array) && !count($array[$j])) $j++; while (count($array[$i]) && isset($array[$j]) && count($array[$j])) { $pairs[] = array(array_shift($array[$i]), array_shift($array[$j])); // exhausted other arrays elements? while ($j < count($array) && !count($array[$j])) $j++; } } return $pairs; } $array = array ( 'parent_id' => array ( 0 => array ( 0 => 12 ), 1 => array ( 0 => 22 ), 2 => array ( 0 => 23, 1 => 24 ) ) ); print_r(find_pairs($array)); $array = array ( 'parent_id' => array ( 9 => array ( 0 => 12 ), 20 => array ( 0 => 25, 1 => 22 ), 21 => array ( 0 => 23, 1 => 24 ) ) ); print_r(find_pairs($array)); $array = array ( 'parent_id' => array ( 9 => array ( 0 => 11, 1 => 12 ), 10 => array ( 0 => 13, 1 => 14 ), 20 => array ( 0 => 21, 1 => 22 ), 21 => array ( 0 => 23 ) ) ); print_r(find_pairs($array));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dcKnX
function name:  (null)
number of ops:  22
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   ASSIGN                                                   !0, <array>
   40     1        INIT_FCALL                                               'print_r'
          2        INIT_FCALL                                               'find_pairs'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
   42     7        ASSIGN                                                   !0, <array>
   61     8        INIT_FCALL                                               'print_r'
          9        INIT_FCALL                                               'find_pairs'
         10        SEND_VAR                                                 !0
         11        DO_FCALL                                      0  $5      
         12        SEND_VAR                                                 $5
         13        DO_ICALL                                                 
   63    14        ASSIGN                                                   !0, <array>
   87    15        INIT_FCALL                                               'print_r'
         16        INIT_FCALL                                               'find_pairs'
         17        SEND_VAR                                                 !0
         18        DO_FCALL                                      0  $8      
         19        SEND_VAR                                                 $8
         20        DO_ICALL                                                 
         21      > RETURN                                                   1

Function find_pairs:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 67
Branch analysis from position: 67
2 jumps found. (Code = 44) Position 1 = 71, Position 2 = 15
Branch analysis from position: 71
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 20
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 66
Branch analysis from position: 66
2 jumps found. (Code = 44) Position 1 = 71, Position 2 = 15
Branch analysis from position: 71
Branch analysis from position: 15
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
2 jumps found. (Code = 47) Position 1 = 24, Position 2 = 32
Branch analysis from position: 24
2 jumps found. (Code = 46) Position 1 = 27, Position 2 = 31
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 21
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 56
Branch analysis from position: 56
2 jumps found. (Code = 46) Position 1 = 59, Position 2 = 61
Branch analysis from position: 59
2 jumps found. (Code = 46) Position 1 = 62, Position 2 = 65
Branch analysis from position: 62
2 jumps found. (Code = 44) Position 1 = 66, Position 2 = 34
Branch analysis from position: 66
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
2 jumps found. (Code = 46) Position 1 = 51, Position 2 = 55
Branch analysis from position: 51
2 jumps found. (Code = 44) Position 1 = 56, Position 2 = 47
Branch analysis from position: 56
Branch analysis from position: 47
2 jumps found. (Code = 46) Position 1 = 51, Position 2 = 55
Branch analysis from position: 51
Branch analysis from position: 55
Branch analysis from position: 55
Branch analysis from position: 65
Branch analysis from position: 61
Branch analysis from position: 21
2 jumps found. (Code = 47) Position 1 = 24, Position 2 = 32
Branch analysis from position: 24
Branch analysis from position: 32
Branch analysis from position: 31
Branch analysis from position: 32
filename:       /in/dcKnX
function name:  find_pairs
number of ops:  73
compiled vars:  !0 = $array, !1 = $pairs, !2 = $i, !3 = $j
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        INIT_FCALL                                               'array_values'
          2        FETCH_DIM_R                                      ~4      !0, 'parent_id'
          3        SEND_VAL                                                 ~4
          4        DO_ICALL                                         $5      
          5        ASSIGN                                                   !0, $5
    7     6        INIT_FCALL                                               'usort'
          7        SEND_REF                                                 !0
          8        DECLARE_LAMBDA_FUNCTION                          ~7      [0]
          9        SEND_VAL                                                 ~7
         10        DO_ICALL                                                 
    9    11        ASSIGN                                                   !1, <array>
   10    12        ASSIGN                                                   !2, 0
         13        ASSIGN                                                   !3, 1
         14      > JMP                                                      ->67
   11    15    >   FETCH_DIM_R                                      ~12     !0, !2
         16        COUNT                                            ~13     ~12
         17        BOOL_NOT                                         ~14     ~13
         18      > JMPZ                                                     ~14, ->20
         19    > > JMP                                                      ->66
   12    20    > > JMP                                                      ->22
         21    >   PRE_INC                                                  !3
         22    >   IS_SMALLER_OR_EQUAL                              ~16     !3, !2
         23      > JMPNZ_EX                                         ~16     ~16, ->32
         24    >   COUNT                                            ~17     !0
         25        IS_SMALLER                                       ~18     !3, ~17
         26      > JMPZ_EX                                          ~18     ~18, ->31
         27    >   FETCH_DIM_R                                      ~19     !0, !3
         28        COUNT                                            ~20     ~19
         29        BOOL_NOT                                         ~21     ~20
         30        BOOL                                             ~18     ~21
         31    >   BOOL                                             ~16     ~18
         32    > > JMPNZ                                                    ~16, ->21
   13    33    > > JMP                                                      ->56
   14    34    >   INIT_FCALL                                               'array_shift'
         35        FETCH_DIM_W                                      $23     !0, !2
         36        SEND_REF                                                 $23
         37        DO_ICALL                                         $24     
         38        INIT_ARRAY                                       ~25     $24
         39        INIT_FCALL                                               'array_shift'
         40        FETCH_DIM_W                                      $26     !0, !3
         41        SEND_REF                                                 $26
         42        DO_ICALL                                         $27     
         43        ADD_ARRAY_ELEMENT                                ~25     $27
         44        ASSIGN_DIM                                               !1
         45        OP_DATA                                                  ~25
   16    46      > JMP                                                      ->48
         47    >   PRE_INC                                                  !3
         48    >   COUNT                                            ~29     !0
         49        IS_SMALLER                                       ~30     !3, ~29
         50      > JMPZ_EX                                          ~30     ~30, ->55
         51    >   FETCH_DIM_R                                      ~31     !0, !3
         52        COUNT                                            ~32     ~31
         53        BOOL_NOT                                         ~33     ~32
         54        BOOL                                             ~30     ~33
         55    > > JMPNZ                                                    ~30, ->47
   13    56    >   FETCH_DIM_R                                      ~34     !0, !2
         57        COUNT                                            ~35     ~34
         58      > JMPZ_EX                                          ~35     ~35, ->61
         59    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~36     !0, !3
         60        BOOL                                             ~35     ~36
         61    > > JMPZ_EX                                          ~35     ~35, ->65
         62    >   FETCH_DIM_R                                      ~37     !0, !3
         63        COUNT                                            ~38     ~37
         64        BOOL                                             ~35     ~38
         65    > > JMPNZ                                                    ~35, ->34
   10    66    >   PRE_INC                                                  !2
         67    >   COUNT                                            ~40     !0
         68        SUB                                              ~41     ~40, 1
         69        IS_SMALLER                                               !2, ~41
         70      > JMPNZ                                                    ~42, ->15
   19    71    > > RETURN                                                   !1
   20    72*     > RETURN                                                   null


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

End of Dynamic Function 0

End of function find_pairs

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
144.59 ms | 1014 KiB | 20 Q