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; $threshold_word = $threshold; $lev_point_value = 0; $merged=[]; foreach($new as $key => $story){ // 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) { $threshold_word++; // If the count of words from preg_grep is above threshold it's mergable if(count(preg_grep($words, $story2["keywords"])) > $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($res);
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 = 16, Position 2 = 114
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 114
Branch analysis from position: 17
2 jumps found. (Code = 77) Position 1 = 27, Position 2 = 112
Branch analysis from position: 27
2 jumps found. (Code = 78) Position 1 = 28, Position 2 = 112
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 = 111
Branch analysis from position: 41
2 jumps found. (Code = 77) Position 1 = 43, Position 2 = 110
Branch analysis from position: 43
2 jumps found. (Code = 78) Position 1 = 44, Position 2 = 110
Branch analysis from position: 44
2 jumps found. (Code = 77) Position 1 = 46, Position 2 = 108
Branch analysis from position: 46
2 jumps found. (Code = 78) Position 1 = 47, Position 2 = 108
Branch analysis from position: 47
2 jumps found. (Code = 43) Position 1 = 53, Position 2 = 107
Branch analysis from position: 53
2 jumps found. (Code = 43) Position 1 = 62, Position 2 = 107
Branch analysis from position: 62
2 jumps found. (Code = 43) Position 1 = 65, Position 2 = 67
Branch analysis from position: 65
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
Branch analysis from position: 67
Branch analysis from position: 107
Branch analysis from position: 107
Branch analysis from position: 108
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Branch analysis from position: 108
Branch analysis from position: 110
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 110
Branch analysis from position: 111
Branch analysis from position: 40
Branch analysis from position: 33
Branch analysis from position: 112
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 112
Branch analysis from position: 114
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 114
Branch analysis from position: 10
filename:       /in/QCXhR
function name:  (null)
number of ops:  119
compiled vars:  !0 = $array, !1 = $new, !2 = $subarr, !3 = $threshold, !4 = $threshold_word, !5 = $lev_point_value, !6 = $merged, !7 = $story, !8 = $key, !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, !3
   53    13        ASSIGN                                                   !5, 0
   54    14        ASSIGN                                                   !6, <array>
   55    15      > FE_RESET_R                                       $24     !1, ->114
         16    > > FE_FETCH_R                                       ~25     $24, !7, ->114
         17    >   ASSIGN                                                   !8, ~25
   57    18        INIT_FCALL                                               'implode'
         19        SEND_VAL                                                 '%7C'
         20        FETCH_DIM_R                                      ~27     !7, '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, ->112
         27    > > FE_FETCH_R                                       ~33     $32, !10, ->112
         28    >   ASSIGN                                                   !11, ~33
   60    29        IS_NOT_EQUAL                                     ~35     !8, !11
         30      > JMPZ_EX                                          ~35     ~35, ->33
         31    >   IS_SMALLER                                       ~36     !8, !11
         32        BOOL                                             ~35     ~36
         33    > > JMPZ_EX                                          ~35     ~35, ->40
         34    >   INIT_FCALL                                               'in_array'
         35        SEND_VAR                                                 !11
         36        SEND_VAR                                                 !6
         37        DO_ICALL                                         $37     
         38        BOOL_NOT                                         ~38     $37
         39        BOOL                                             ~35     ~38
         40    > > JMPZ                                                     ~35, ->111
   61    41    >   FETCH_DIM_R                                      ~39     !7, 'keywords'
         42      > FE_RESET_R                                       $40     ~39, ->110
         43    > > FE_FETCH_R                                               $40, !12, ->110
   62    44    >   FETCH_DIM_R                                      ~41     !10, 'keywords'
         45      > FE_RESET_R                                       $42     ~41, ->108
         46    > > FE_FETCH_R                                               $42, !13, ->108
   63    47    >   INIT_FCALL                                               'levenshtein'
         48        SEND_VAR                                                 !12
         49        SEND_VAR                                                 !13
         50        DO_ICALL                                         $43     
         51        IS_SMALLER_OR_EQUAL                                      $43, !5
         52      > JMPZ                                                     ~44, ->107
   64    53    >   PRE_INC                                                  !4
   66    54        INIT_FCALL                                               'preg_grep'
         55        SEND_VAR                                                 !9
         56        FETCH_DIM_R                                      ~46     !10, 'keywords'
         57        SEND_VAL                                                 ~46
         58        DO_ICALL                                         $47     
         59        COUNT                                            ~48     $47
         60        IS_SMALLER                                               !3, ~48
         61      > JMPZ                                                     ~49, ->107
   72    62    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~50     !14, !8
         63        BOOL_NOT                                         ~51     ~50
         64      > JMPZ                                                     ~51, ->67
         65    >   ASSIGN_DIM                                               !14, !8
         66        OP_DATA                                                  <array>
   75    67    >   FETCH_DIM_R                                      ~56     !7, 'header'
         68        FETCH_DIM_W                                      $53     !14, !8
         69        FETCH_DIM_W                                      $54     $53, 'header'
         70        ASSIGN_DIM                                               $54
         71        OP_DATA                                                  ~56
   76    72        FETCH_DIM_R                                      ~60     !10, 'header'
         73        FETCH_DIM_W                                      $57     !14, !8
         74        FETCH_DIM_W                                      $58     $57, 'header'
         75        ASSIGN_DIM                                               $58
         76        OP_DATA                                                  ~60
   79    77        INIT_FCALL                                               'array_unique'
         78        FETCH_DIM_R                                      ~63     !14, !8
         79        FETCH_DIM_R                                      ~64     ~63, 'header'
         80        SEND_VAL                                                 ~64
         81        DO_ICALL                                         $65     
         82        FETCH_DIM_W                                      $61     !14, !8
         83        ASSIGN_DIM                                               $61, 'header'
         84        OP_DATA                                                  $65
   82    85        INIT_FCALL                                               'array_merge'
         86        FETCH_DIM_R                                      ~68     !14, !8
         87        FETCH_DIM_R                                      ~69     ~68, 'keywords'
         88        SEND_VAL                                                 ~69
         89        FETCH_DIM_R                                      ~70     !7, 'keywords'
         90        SEND_VAL                                                 ~70
         91        FETCH_DIM_R                                      ~71     !10, 'keywords'
         92        SEND_VAL                                                 ~71
         93        DO_ICALL                                         $72     
         94        FETCH_DIM_W                                      $66     !14, !8
         95        ASSIGN_DIM                                               $66, 'keywords'
         96        OP_DATA                                                  $72
   83    97        INIT_FCALL                                               'array_unique'
         98        FETCH_DIM_R                                      ~75     !14, !8
         99        FETCH_DIM_R                                      ~76     ~75, 'keywords'
        100        SEND_VAL                                                 ~76
        101        DO_ICALL                                         $77     
        102        FETCH_DIM_W                                      $73     !14, !8
        103        ASSIGN_DIM                                               $73, 'keywords'
        104        OP_DATA                                                  $77
   86   105        ASSIGN_DIM                                               !6
        106        OP_DATA                                                  !11
   62   107    > > JMP                                                      ->46
        108    >   FE_FREE                                                  $42
   61   109      > JMP                                                      ->43
        110    >   FE_FREE                                                  $40
   58   111    > > JMP                                                      ->27
        112    >   FE_FREE                                                  $32
   55   113      > JMP                                                      ->16
        114    >   FE_FREE                                                  $24
   95   115        INIT_FCALL                                               'var_dump'
        116        SEND_VAR                                                 !14
        117        DO_ICALL                                                 
        118      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
169.77 ms | 1408 KiB | 27 Q