3v4l.org

run code in 300+ PHP versions simultaneously
<?php function buildPedigree($haystack, $akc_reg_num, $generation = 0) { ++$generation; foreach ($haystack as $index => $row) { if ($row['akc_reg_num'] == $akc_reg_num) { $result = ['sire' => $row['akc_parent_sire'], 'dam' => $row['akc_parent_dam']]; unset($haystack[$index]); // reduce the haystack to improve efficiency and avoid infinite loop break; } } if (!isset($result)) { return []; // $akc_reg_num not found } foreach ($haystack as $row) { if ($row['akc_reg_num'] == $result['sire']) { $result['sire_parents'] = array_filter(buildPedigree($haystack, $row['akc_reg_num'], $generation)); // recurse and purge empty parent arrays if (array_key_exists('dam_parents', $result)) { break; // both parents found in generation, stop this loop } } elseif ($row['akc_reg_num'] == $result['dam']) { $result['dam_parents'] = array_filter(buildPedigree($haystack, $row['akc_reg_num'], $generation)); // recurse and purge empty parent arrays if (array_key_exists('sire_parents', $result)) { break; // both parents found in generation, stop this loop } } } return $generation <= 4 ? $result : []; } $animals = [ ['id' => 1, 'akc_reg_num' => 1, 'akc_parent_sire' => 2, 'akc_parent_dam' => 3], ['id' => 2, 'akc_reg_num' => 2, 'akc_parent_sire' => 5, 'akc_parent_dam' => 6], ['id' => 3, 'akc_reg_num' => 3, 'akc_parent_sire' => 9, 'akc_parent_dam' => 0], ['id' => 4, 'akc_reg_num' => 5, 'akc_parent_sire' => 0, 'akc_parent_dam' => 0], ['id' => 5, 'akc_reg_num' => 6, 'akc_parent_sire' => 7, 'akc_parent_dam' => 8], ['id' => 6, 'akc_reg_num' => 7, 'akc_parent_sire' => 0, 'akc_parent_dam' => 0], ['id' => 7, 'akc_reg_num' => 8, 'akc_parent_sire' => 0, 'akc_parent_dam' => 0], ['id' => 8, 'akc_reg_num' => 9, 'akc_parent_sire' => 10, 'akc_parent_dam' => 11], ['id' => 9, 'akc_reg_num' => 10, 'akc_parent_sire' => 0, 'akc_parent_dam' => 0], ['id' => 10, 'akc_reg_num' => 11, 'akc_parent_sire' => 12, 'akc_parent_dam' => 0], ['id' => 11, 'akc_reg_num' => 12, 'akc_parent_sire' => 0, 'akc_parent_dam' => 0] ]; var_export(buildPedigree($animals, 1));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HMnU5
function name:  (null)
number of ops:  9
compiled vars:  !0 = $animals
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   ASSIGN                                                   !0, <array>
   45     1        INIT_FCALL                                               'var_export'
          2        INIT_FCALL                                               'buildpedigree'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 1
          5        DO_FCALL                                      0  $2      
          6        SEND_VAR                                                 $2
          7        DO_ICALL                                                 
          8      > RETURN                                                   1

Function buildpedigree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 18
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 18
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 17
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 23
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
2 jumps found. (Code = 77) Position 1 = 24, Position 2 = 65
Branch analysis from position: 24
2 jumps found. (Code = 78) Position 1 = 25, Position 2 = 65
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 45
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 43, Position 2 = 44
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
2 jumps found. (Code = 43) Position 1 = 68, Position 2 = 70
Branch analysis from position: 68
1 jumps found. (Code = 42) Position 1 = 71
Branch analysis from position: 71
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 70
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 44
1 jumps found. (Code = 42) Position 1 = 64
Branch analysis from position: 64
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 45
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 64
Branch analysis from position: 49
2 jumps found. (Code = 43) Position 1 = 63, Position 2 = 64
Branch analysis from position: 63
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
Branch analysis from position: 64
Branch analysis from position: 64
Branch analysis from position: 65
Branch analysis from position: 65
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 18
Branch analysis from position: 18
filename:       /in/HMnU5
function name:  buildPedigree
number of ops:  73
compiled vars:  !0 = $haystack, !1 = $akc_reg_num, !2 = $generation, !3 = $row, !4 = $index, !5 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      0
    3     3        PRE_INC                                                  !2
    4     4      > FE_RESET_R                                       $7      !0, ->18
          5    > > FE_FETCH_R                                       ~8      $7, !3, ->18
          6    >   ASSIGN                                                   !4, ~8
    5     7        FETCH_DIM_R                                      ~10     !3, 'akc_reg_num'
          8        IS_EQUAL                                                 !1, ~10
          9      > JMPZ                                                     ~11, ->17
    6    10    >   FETCH_DIM_R                                      ~12     !3, 'akc_parent_sire'
         11        INIT_ARRAY                                       ~13     ~12, 'sire'
         12        FETCH_DIM_R                                      ~14     !3, 'akc_parent_dam'
         13        ADD_ARRAY_ELEMENT                                ~13     ~14, 'dam'
         14        ASSIGN                                                   !5, ~13
    7    15        UNSET_DIM                                                !0, !4
    8    16      > JMP                                                      ->18
    4    17    > > JMP                                                      ->5
         18    >   FE_FREE                                                  $7
   11    19        ISSET_ISEMPTY_CV                                 ~16     !5
         20        BOOL_NOT                                         ~17     ~16
         21      > JMPZ                                                     ~17, ->23
   12    22    > > RETURN                                                   <array>
   15    23    > > FE_RESET_R                                       $18     !0, ->65
         24    > > FE_FETCH_R                                               $18, !3, ->65
   16    25    >   FETCH_DIM_R                                      ~19     !3, 'akc_reg_num'
         26        FETCH_DIM_R                                      ~20     !5, 'sire'
         27        IS_EQUAL                                                 ~19, ~20
         28      > JMPZ                                                     ~21, ->45
   17    29    >   INIT_FCALL                                               'array_filter'
         30        INIT_FCALL_BY_NAME                                       'buildPedigree'
         31        SEND_VAR_EX                                              !0
         32        CHECK_FUNC_ARG                                           
         33        FETCH_DIM_FUNC_ARG                               $23     !3, 'akc_reg_num'
         34        SEND_FUNC_ARG                                            $23
         35        SEND_VAR_EX                                              !2
         36        DO_FCALL                                      0  $24     
         37        SEND_VAR                                                 $24
         38        DO_ICALL                                         $25     
         39        ASSIGN_DIM                                               !5, 'sire_parents'
         40        OP_DATA                                                  $25
   18    41        ARRAY_KEY_EXISTS                                         'dam_parents', !5
         42      > JMPZ                                                     ~26, ->44
   19    43    > > JMP                                                      ->65
   16    44    > > JMP                                                      ->64
   21    45    >   FETCH_DIM_R                                      ~27     !3, 'akc_reg_num'
         46        FETCH_DIM_R                                      ~28     !5, 'dam'
         47        IS_EQUAL                                                 ~27, ~28
         48      > JMPZ                                                     ~29, ->64
   22    49    >   INIT_FCALL                                               'array_filter'
         50        INIT_FCALL_BY_NAME                                       'buildPedigree'
         51        SEND_VAR_EX                                              !0
         52        CHECK_FUNC_ARG                                           
         53        FETCH_DIM_FUNC_ARG                               $31     !3, 'akc_reg_num'
         54        SEND_FUNC_ARG                                            $31
         55        SEND_VAR_EX                                              !2
         56        DO_FCALL                                      0  $32     
         57        SEND_VAR                                                 $32
         58        DO_ICALL                                         $33     
         59        ASSIGN_DIM                                               !5, 'dam_parents'
         60        OP_DATA                                                  $33
   23    61        ARRAY_KEY_EXISTS                                         'sire_parents', !5
         62      > JMPZ                                                     ~34, ->64
   24    63    > > JMP                                                      ->65
   15    64    > > JMP                                                      ->24
         65    >   FE_FREE                                                  $18
   28    66        IS_SMALLER_OR_EQUAL                                      !2, 4
         67      > JMPZ                                                     ~35, ->70
         68    >   QM_ASSIGN                                        ~36     !5
         69      > JMP                                                      ->71
         70    >   QM_ASSIGN                                        ~36     <array>
         71    > > RETURN                                                   ~36
   29    72*     > RETURN                                                   null

End of function buildpedigree

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
138.85 ms | 1010 KiB | 16 Q