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(); $c = count($array); $i = 0; // loop while there are enough values to pair (2 or more) while (array_reduce($array, function ($c, $v) { return $c + count($v); }, 0) > 1) { // make sure there are some elements in this array while (!count($array[$i])) $i = ($i + 1) % $c; // find the next array with a value $j = ($i + 1) % $c; while (!count($array[$j])) $j = ($j + 1) % $c; // have we come full circle? if ($j == $i) break; // save the pair $pairs[] = array(array_shift($array[$i]), array_shift($array[$j])); // move on to the next array $i = ($i + 1) % $c; } return $pairs; } $array = array ('parent_id' => array([1])); print_r(find_pairs($array)); $array = array ('parent_id' => array([1], [3])); print_r(find_pairs($array)); $array = array ('parent_id' => array([1], [3], [5])); print_r(find_pairs($array)); $array = array ('parent_id' => array([1], [3], [5], [7])); print_r(find_pairs($array)); $array = array ('parent_id' => array([1, 2])); print_r(find_pairs($array)); $array = array ('parent_id' => array([1, 2], [3])); print_r(find_pairs($array)); $array = array ('parent_id' => array([1, 2], [3], [5])); print_r(find_pairs($array)); $array = array ('parent_id' => array([1, 2], [3], [5], [7])); print_r(find_pairs($array)); $array = array ('parent_id' => array([1, 2], [3, 4])); print_r(find_pairs($array)); $array = array ('parent_id' => array([1, 2], [3, 4], [5])); print_r(find_pairs($array)); $array = array ('parent_id' => array([1, 2], [3, 4], [5], [7])); print_r(find_pairs($array)); $array = array ('parent_id' => array([1, 2], [3, 4], [5, 6])); print_r(find_pairs($array)); $array = array ('parent_id' => array([1, 2], [3, 4], [5, 6], [7])); print_r(find_pairs($array)); $array = array ('parent_id' => array([1, 2], [3, 4], [5, 6], [7, 8])); print_r(find_pairs($array));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kK4Zo
function name:  (null)
number of ops:  99
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   ASSIGN                                                   !0, <array>
   30     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                                                 
   31     7        ASSIGN                                                   !0, <array>
   32     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                                                 
   33    14        ASSIGN                                                   !0, <array>
   34    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                                                 
   35    21        ASSIGN                                                   !0, <array>
   36    22        INIT_FCALL                                               'print_r'
         23        INIT_FCALL                                               'find_pairs'
         24        SEND_VAR                                                 !0
         25        DO_FCALL                                      0  $11     
         26        SEND_VAR                                                 $11
         27        DO_ICALL                                                 
   37    28        ASSIGN                                                   !0, <array>
   38    29        INIT_FCALL                                               'print_r'
         30        INIT_FCALL                                               'find_pairs'
         31        SEND_VAR                                                 !0
         32        DO_FCALL                                      0  $14     
         33        SEND_VAR                                                 $14
         34        DO_ICALL                                                 
   39    35        ASSIGN                                                   !0, <array>
   40    36        INIT_FCALL                                               'print_r'
         37        INIT_FCALL                                               'find_pairs'
         38        SEND_VAR                                                 !0
         39        DO_FCALL                                      0  $17     
         40        SEND_VAR                                                 $17
         41        DO_ICALL                                                 
   41    42        ASSIGN                                                   !0, <array>
   42    43        INIT_FCALL                                               'print_r'
         44        INIT_FCALL                                               'find_pairs'
         45        SEND_VAR                                                 !0
         46        DO_FCALL                                      0  $20     
         47        SEND_VAR                                                 $20
         48        DO_ICALL                                                 
   43    49        ASSIGN                                                   !0, <array>
   44    50        INIT_FCALL                                               'print_r'
         51        INIT_FCALL                                               'find_pairs'
         52        SEND_VAR                                                 !0
         53        DO_FCALL                                      0  $23     
         54        SEND_VAR                                                 $23
         55        DO_ICALL                                                 
   45    56        ASSIGN                                                   !0, <array>
   46    57        INIT_FCALL                                               'print_r'
         58        INIT_FCALL                                               'find_pairs'
         59        SEND_VAR                                                 !0
         60        DO_FCALL                                      0  $26     
         61        SEND_VAR                                                 $26
         62        DO_ICALL                                                 
   47    63        ASSIGN                                                   !0, <array>
   48    64        INIT_FCALL                                               'print_r'
         65        INIT_FCALL                                               'find_pairs'
         66        SEND_VAR                                                 !0
         67        DO_FCALL                                      0  $29     
         68        SEND_VAR                                                 $29
         69        DO_ICALL                                                 
   49    70        ASSIGN                                                   !0, <array>
   50    71        INIT_FCALL                                               'print_r'
         72        INIT_FCALL                                               'find_pairs'
         73        SEND_VAR                                                 !0
         74        DO_FCALL                                      0  $32     
         75        SEND_VAR                                                 $32
         76        DO_ICALL                                                 
   51    77        ASSIGN                                                   !0, <array>
   52    78        INIT_FCALL                                               'print_r'
         79        INIT_FCALL                                               'find_pairs'
         80        SEND_VAR                                                 !0
         81        DO_FCALL                                      0  $35     
         82        SEND_VAR                                                 $35
         83        DO_ICALL                                                 
   53    84        ASSIGN                                                   !0, <array>
   54    85        INIT_FCALL                                               'print_r'
         86        INIT_FCALL                                               'find_pairs'
         87        SEND_VAR                                                 !0
         88        DO_FCALL                                      0  $38     
         89        SEND_VAR                                                 $38
         90        DO_ICALL                                                 
   55    91        ASSIGN                                                   !0, <array>
   56    92        INIT_FCALL                                               'print_r'
         93        INIT_FCALL                                               'find_pairs'
         94        SEND_VAR                                                 !0
         95        DO_FCALL                                      0  $41     
         96        SEND_VAR                                                 $41
         97        DO_ICALL                                                 
         98      > RETURN                                                   1

Function find_pairs:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 53
Branch analysis from position: 53
2 jumps found. (Code = 44) Position 1 = 61, Position 2 = 16
Branch analysis from position: 61
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 24, Position 2 = 17
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
2 jumps found. (Code = 44) Position 1 = 35, Position 2 = 28
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 38
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 61
Branch analysis from position: 61
Branch analysis from position: 38
2 jumps found. (Code = 44) Position 1 = 61, Position 2 = 16
Branch analysis from position: 61
Branch analysis from position: 16
Branch analysis from position: 28
2 jumps found. (Code = 44) Position 1 = 35, Position 2 = 28
Branch analysis from position: 35
Branch analysis from position: 28
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 24, Position 2 = 17
Branch analysis from position: 24
Branch analysis from position: 17
filename:       /in/kK4Zo
function name:  find_pairs
number of ops:  63
compiled vars:  !0 = $array, !1 = $pairs, !2 = $c, !3 = $i, !4 = $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                                      ~5      !0, 'parent_id'
          3        SEND_VAL                                                 ~5
          4        DO_ICALL                                         $6      
          5        ASSIGN                                                   !0, $6
    7     6        INIT_FCALL                                               'usort'
          7        SEND_REF                                                 !0
          8        DECLARE_LAMBDA_FUNCTION                          ~8      [0]
          9        SEND_VAL                                                 ~8
         10        DO_ICALL                                                 
    9    11        ASSIGN                                                   !1, <array>
   10    12        COUNT                                            ~11     !0
         13        ASSIGN                                                   !2, ~11
   11    14        ASSIGN                                                   !3, 0
   13    15      > JMP                                                      ->53
   15    16    > > JMP                                                      ->20
         17    >   ADD                                              ~14     !3, 1
         18        MOD                                              ~15     ~14, !2
         19        ASSIGN                                                   !3, ~15
         20    >   FETCH_DIM_R                                      ~17     !0, !3
         21        COUNT                                            ~18     ~17
         22        BOOL_NOT                                         ~19     ~18
         23      > JMPNZ                                                    ~19, ->17
   17    24    >   ADD                                              ~20     !3, 1
         25        MOD                                              ~21     ~20, !2
         26        ASSIGN                                                   !4, ~21
   18    27      > JMP                                                      ->31
         28    >   ADD                                              ~23     !4, 1
         29        MOD                                              ~24     ~23, !2
         30        ASSIGN                                                   !4, ~24
         31    >   FETCH_DIM_R                                      ~26     !0, !4
         32        COUNT                                            ~27     ~26
         33        BOOL_NOT                                         ~28     ~27
         34      > JMPNZ                                                    ~28, ->28
   20    35    >   IS_EQUAL                                                 !4, !3
         36      > JMPZ                                                     ~29, ->38
         37    > > JMP                                                      ->61
   22    38    >   INIT_FCALL                                               'array_shift'
         39        FETCH_DIM_W                                      $31     !0, !3
         40        SEND_REF                                                 $31
         41        DO_ICALL                                         $32     
         42        INIT_ARRAY                                       ~33     $32
         43        INIT_FCALL                                               'array_shift'
         44        FETCH_DIM_W                                      $34     !0, !4
         45        SEND_REF                                                 $34
         46        DO_ICALL                                         $35     
         47        ADD_ARRAY_ELEMENT                                ~33     $35
         48        ASSIGN_DIM                                               !1
         49        OP_DATA                                                  ~33
   24    50        ADD                                              ~36     !3, 1
         51        MOD                                              ~37     ~36, !2
         52        ASSIGN                                                   !3, ~37
   13    53    >   INIT_FCALL                                               'array_reduce'
         54        SEND_VAR                                                 !0
         55        DECLARE_LAMBDA_FUNCTION                          ~39     [1]
         56        SEND_VAL                                                 ~39
         57        SEND_VAL                                                 0
         58        DO_ICALL                                         $40     
         59        IS_SMALLER                                               1, $40
         60      > JMPNZ                                                    ~41, ->16
   26    61    > > RETURN                                                   !1
   27    62*     > 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/kK4Zo
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

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

End of Dynamic Function 1

End of function find_pairs

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
142.49 ms | 1023 KiB | 32 Q