3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = array ( // Sub-array 1 array ( // Story 'Monkey 1' - Has identical sub-sub-arrays 'Monkey 2' and 'Monkey 3' and will be merged with them. array ( "header" => "This is a story about a monkey.", 'keywords' => array( "Trees", "Monkey", "Flying", "Drink", "Vacation", "Coconut", "Big", "Bonobo", "Climbing") ), // Story 'Cat 1' - Has identical sub-sub-array 'Cat 2' and will be merged with it. array ( "header" => "Here's a catarific story about a cat", 'keywords' => array( "meauw", "raaaw", "kitty", "growup", "Fun", "claws", "fish", "salmon") ) ), // Sub-array 2 array ( // Story 'Monkey 2' - Has identical sub-sub-arrays 'Monkey 1' and 'Monkey 3' and will be merged with them. array ( "header" => "This is another, but different story, about a monkey.", 'keywords' => array( "Monkey", "Big", "Trees", "Bonobo", "Fun", "Dance", "Cow", "Coconuts") ), // Story 'Cat 2' - Has identical sub-sub-array 'Cat 1' and will be merged with it. array ( "header" => "Here's a different story about a cat", 'keywords' => array( "meauwe", "ball", "cat", "kitten", "claws", "sleep", "fish", "purr") ) ), // Sub-array 3 array ( // Story 'Monkey 3' - Has identical sub-sub-arrays 'Monkey 1' and 'Monkey 2' and will be merged with them. array ( "header" => "This is a third story about a monkey.", 'keywords' => array( "Jungle", "tree", "monkey", "Bonobo", "Fun", "Dance", "climbing", "Coconut", "pretty") ), // Story 'Fireman 1' - Has no identical sub-sub-arrays and will not be merged. array ( "header" => "This is a story about a fireman", 'keywords' => array( "fire", "explosion", "burning", "rescue", "happy", "help", "water", "car") ) ) ); //flatten array to make it simpler $new =[]; foreach($array as $subarr){ $new = array_merge($new, $subarr); } $threshold = 3; $lev_point_value = 1; $merged=[]; foreach($new as $key => $story){ $word_count = 0; // create regex pattern to find similar items $words = "/" . implode("|", $story["keywords"]) . "/i"; foreach($new as $key2 => $story2){ // only loop new items and items that has not been merged already if($key != $key2 && $key2 > $key && !in_array($key2, $merged)){ foreach ($story['keywords'] as $item1){ foreach ($story2["keywords"] as $item2){ if (levenshtein($item1, $item2) <= $lev_point_value) { $word_count++; // If the count of words from preg_grep is above threshold it's mergable if($word_count >= $threshold){ // debug //echo $key . " " . $key2 . "\n"; //echo $story["header"] . " = " . $story2["header"] ."\n\n"; // if the item does not exist create it first to remove notices if(!isset($res[$key])) $res[$key] = ["header" => [], "keywords" =>[]]; // add headers $res[$key]["header"][] = $story["header"]; $res[$key]["header"][] = $story2["header"]; // only keep unique $res[$key]["header"] = array_unique($res[$key]["header"]); // add keywords and remove duplicates $res[$key]["keywords"] = array_merge($res[$key]["keywords"], $story["keywords"], $story2["keywords"]); $res[$key]["keywords"] = array_unique($res[$key]["keywords"]); // add key2 to merged so that we don't merge this again. $merged[] = $key2; } } } } } } } var_dump($new);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 10
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 10
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 10
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 108
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 108
Branch analysis from position: 16
2 jumps found. (Code = 77) Position 1 = 27, Position 2 = 106
Branch analysis from position: 27
2 jumps found. (Code = 78) Position 1 = 28, Position 2 = 106
Branch analysis from position: 28
2 jumps found. (Code = 46) Position 1 = 31, Position 2 = 33
Branch analysis from position: 31
2 jumps found. (Code = 46) Position 1 = 34, Position 2 = 40
Branch analysis from position: 34
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 105
Branch analysis from position: 41
2 jumps found. (Code = 77) Position 1 = 43, Position 2 = 104
Branch analysis from position: 43
2 jumps found. (Code = 78) Position 1 = 44, Position 2 = 104
Branch analysis from position: 44
2 jumps found. (Code = 77) Position 1 = 46, Position 2 = 102
Branch analysis from position: 46
2 jumps found. (Code = 78) Position 1 = 47, Position 2 = 102
Branch analysis from position: 47
2 jumps found. (Code = 43) Position 1 = 53, Position 2 = 101
Branch analysis from position: 53
2 jumps found. (Code = 43) Position 1 = 56, Position 2 = 101
Branch analysis from position: 56
2 jumps found. (Code = 43) Position 1 = 59, Position 2 = 61
Branch analysis from position: 59
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
Branch analysis from position: 61
Branch analysis from position: 101
Branch analysis from position: 101
Branch analysis from position: 102
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Branch analysis from position: 102
Branch analysis from position: 104
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 104
Branch analysis from position: 105
Branch analysis from position: 40
Branch analysis from position: 33
Branch analysis from position: 106
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 106
Branch analysis from position: 108
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 108
Branch analysis from position: 10
filename:       /in/3H8kH
function name:  (null)
number of ops:  113
compiled vars:  !0 = $array, !1 = $new, !2 = $subarr, !3 = $threshold, !4 = $lev_point_value, !5 = $merged, !6 = $story, !7 = $key, !8 = $word_count, !9 = $words, !10 = $story2, !11 = $key2, !12 = $item1, !13 = $item2, !14 = $res
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   46     1        ASSIGN                                                   !1, <array>
   47     2      > FE_RESET_R                                       $17     !0, ->10
          3    > > FE_FETCH_R                                               $17, !2, ->10
   48     4    >   INIT_FCALL                                               'array_merge'
          5        SEND_VAR                                                 !1
          6        SEND_VAR                                                 !2
          7        DO_ICALL                                         $18     
          8        ASSIGN                                                   !1, $18
   47     9      > JMP                                                      ->3
         10    >   FE_FREE                                                  $17
   51    11        ASSIGN                                                   !3, 3
   52    12        ASSIGN                                                   !4, 1
   53    13        ASSIGN                                                   !5, <array>
   54    14      > FE_RESET_R                                       $23     !1, ->108
         15    > > FE_FETCH_R                                       ~24     $23, !6, ->108
         16    >   ASSIGN                                                   !7, ~24
   55    17        ASSIGN                                                   !8, 0
   57    18        INIT_FCALL                                               'implode'
         19        SEND_VAL                                                 '%7C'
         20        FETCH_DIM_R                                      ~27     !6, 'keywords'
         21        SEND_VAL                                                 ~27
         22        DO_ICALL                                         $28     
         23        CONCAT                                           ~29     '%2F', $28
         24        CONCAT                                           ~30     ~29, '%2Fi'
         25        ASSIGN                                                   !9, ~30
   58    26      > FE_RESET_R                                       $32     !1, ->106
         27    > > FE_FETCH_R                                       ~33     $32, !10, ->106
         28    >   ASSIGN                                                   !11, ~33
   60    29        IS_NOT_EQUAL                                     ~35     !7, !11
         30      > JMPZ_EX                                          ~35     ~35, ->33
         31    >   IS_SMALLER                                       ~36     !7, !11
         32        BOOL                                             ~35     ~36
         33    > > JMPZ_EX                                          ~35     ~35, ->40
         34    >   INIT_FCALL                                               'in_array'
         35        SEND_VAR                                                 !11
         36        SEND_VAR                                                 !5
         37        DO_ICALL                                         $37     
         38        BOOL_NOT                                         ~38     $37
         39        BOOL                                             ~35     ~38
         40    > > JMPZ                                                     ~35, ->105
   61    41    >   FETCH_DIM_R                                      ~39     !6, 'keywords'
         42      > FE_RESET_R                                       $40     ~39, ->104
         43    > > FE_FETCH_R                                               $40, !12, ->104
   62    44    >   FETCH_DIM_R                                      ~41     !10, 'keywords'
         45      > FE_RESET_R                                       $42     ~41, ->102
         46    > > FE_FETCH_R                                               $42, !13, ->102
   63    47    >   INIT_FCALL                                               'levenshtein'
         48        SEND_VAR                                                 !12
         49        SEND_VAR                                                 !13
         50        DO_ICALL                                         $43     
         51        IS_SMALLER_OR_EQUAL                                      $43, !4
         52      > JMPZ                                                     ~44, ->101
   64    53    >   PRE_INC                                                  !8
   66    54        IS_SMALLER_OR_EQUAL                                      !3, !8
         55      > JMPZ                                                     ~46, ->101
   72    56    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~47     !14, !7
         57        BOOL_NOT                                         ~48     ~47
         58      > JMPZ                                                     ~48, ->61
         59    >   ASSIGN_DIM                                               !14, !7
         60        OP_DATA                                                  <array>
   75    61    >   FETCH_DIM_R                                      ~53     !6, 'header'
         62        FETCH_DIM_W                                      $50     !14, !7
         63        FETCH_DIM_W                                      $51     $50, 'header'
         64        ASSIGN_DIM                                               $51
         65        OP_DATA                                                  ~53
   76    66        FETCH_DIM_R                                      ~57     !10, 'header'
         67        FETCH_DIM_W                                      $54     !14, !7
         68        FETCH_DIM_W                                      $55     $54, 'header'
         69        ASSIGN_DIM                                               $55
         70        OP_DATA                                                  ~57
   79    71        INIT_FCALL                                               'array_unique'
         72        FETCH_DIM_R                                      ~60     !14, !7
         73        FETCH_DIM_R                                      ~61     ~60, 'header'
         74        SEND_VAL                                                 ~61
         75        DO_ICALL                                         $62     
         76        FETCH_DIM_W                                      $58     !14, !7
         77        ASSIGN_DIM                                               $58, 'header'
         78        OP_DATA                                                  $62
   82    79        INIT_FCALL                                               'array_merge'
         80        FETCH_DIM_R                                      ~65     !14, !7
         81        FETCH_DIM_R                                      ~66     ~65, 'keywords'
         82        SEND_VAL                                                 ~66
         83        FETCH_DIM_R                                      ~67     !6, 'keywords'
         84        SEND_VAL                                                 ~67
         85        FETCH_DIM_R                                      ~68     !10, 'keywords'
         86        SEND_VAL                                                 ~68
         87        DO_ICALL                                         $69     
         88        FETCH_DIM_W                                      $63     !14, !7
         89        ASSIGN_DIM                                               $63, 'keywords'
         90        OP_DATA                                                  $69
   83    91        INIT_FCALL                                               'array_unique'
         92        FETCH_DIM_R                                      ~72     !14, !7
         93        FETCH_DIM_R                                      ~73     ~72, 'keywords'
         94        SEND_VAL                                                 ~73
         95        DO_ICALL                                         $74     
         96        FETCH_DIM_W                                      $70     !14, !7
         97        ASSIGN_DIM                                               $70, 'keywords'
         98        OP_DATA                                                  $74
   86    99        ASSIGN_DIM                                               !5
        100        OP_DATA                                                  !11
   62   101    > > JMP                                                      ->46
        102    >   FE_FREE                                                  $42
   61   103      > JMP                                                      ->43
        104    >   FE_FREE                                                  $40
   58   105    > > JMP                                                      ->27
        106    >   FE_FREE                                                  $32
   54   107      > JMP                                                      ->15
        108    >   FE_FREE                                                  $23
   95   109        INIT_FCALL                                               'var_dump'
        110        SEND_VAR                                                 !1
        111        DO_ICALL                                                 
        112      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
181.65 ms | 1408 KiB | 25 Q