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 = 2; $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)){ // 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 = 14, Position 2 = 95
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 95
Branch analysis from position: 15
2 jumps found. (Code = 77) Position 1 = 25, Position 2 = 93
Branch analysis from position: 25
2 jumps found. (Code = 78) Position 1 = 26, Position 2 = 93
Branch analysis from position: 26
2 jumps found. (Code = 46) Position 1 = 29, Position 2 = 31
Branch analysis from position: 29
2 jumps found. (Code = 46) Position 1 = 32, Position 2 = 38
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 92
Branch analysis from position: 39
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 92
Branch analysis from position: 47
2 jumps found. (Code = 43) Position 1 = 50, Position 2 = 52
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
Branch analysis from position: 52
Branch analysis from position: 92
Branch analysis from position: 92
Branch analysis from position: 38
Branch analysis from position: 31
Branch analysis from position: 93
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 93
Branch analysis from position: 95
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 95
Branch analysis from position: 10
filename:       /in/6cKRq
function name:  (null)
number of ops:  100
compiled vars:  !0 = $array, !1 = $new, !2 = $subarr, !3 = $threshold, !4 = $merged, !5 = $story, !6 = $key, !7 = $words, !8 = $story2, !9 = $key2, !10 = $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                                       $13     !0, ->10
          3    > > FE_FETCH_R                                               $13, !2, ->10
   48     4    >   INIT_FCALL                                               'array_merge'
          5        SEND_VAR                                                 !1
          6        SEND_VAR                                                 !2
          7        DO_ICALL                                         $14     
          8        ASSIGN                                                   !1, $14
   47     9      > JMP                                                      ->3
         10    >   FE_FREE                                                  $13
   51    11        ASSIGN                                                   !3, 2
   52    12        ASSIGN                                                   !4, <array>
   53    13      > FE_RESET_R                                       $18     !1, ->95
         14    > > FE_FETCH_R                                       ~19     $18, !5, ->95
         15    >   ASSIGN                                                   !6, ~19
   55    16        INIT_FCALL                                               'implode'
         17        SEND_VAL                                                 '%7C'
         18        FETCH_DIM_R                                      ~21     !5, 'keywords'
         19        SEND_VAL                                                 ~21
         20        DO_ICALL                                         $22     
         21        CONCAT                                           ~23     '%2F', $22
         22        CONCAT                                           ~24     ~23, '%2Fi'
         23        ASSIGN                                                   !7, ~24
   56    24      > FE_RESET_R                                       $26     !1, ->93
         25    > > FE_FETCH_R                                       ~27     $26, !8, ->93
         26    >   ASSIGN                                                   !9, ~27
   58    27        IS_NOT_EQUAL                                     ~29     !6, !9
         28      > JMPZ_EX                                          ~29     ~29, ->31
         29    >   IS_SMALLER                                       ~30     !6, !9
         30        BOOL                                             ~29     ~30
         31    > > JMPZ_EX                                          ~29     ~29, ->38
         32    >   INIT_FCALL                                               'in_array'
         33        SEND_VAR                                                 !9
         34        SEND_VAR                                                 !4
         35        DO_ICALL                                         $31     
         36        BOOL_NOT                                         ~32     $31
         37        BOOL                                             ~29     ~32
         38    > > JMPZ                                                     ~29, ->92
   60    39    >   INIT_FCALL                                               'preg_grep'
         40        SEND_VAR                                                 !7
         41        FETCH_DIM_R                                      ~33     !8, 'keywords'
         42        SEND_VAL                                                 ~33
         43        DO_ICALL                                         $34     
         44        COUNT                                            ~35     $34
         45        IS_SMALLER                                               !3, ~35
         46      > JMPZ                                                     ~36, ->92
   66    47    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~37     !10, !6
         48        BOOL_NOT                                         ~38     ~37
         49      > JMPZ                                                     ~38, ->52
         50    >   ASSIGN_DIM                                               !10, !6
         51        OP_DATA                                                  <array>
   69    52    >   FETCH_DIM_R                                      ~43     !5, 'header'
         53        FETCH_DIM_W                                      $40     !10, !6
         54        FETCH_DIM_W                                      $41     $40, 'header'
         55        ASSIGN_DIM                                               $41
         56        OP_DATA                                                  ~43
   70    57        FETCH_DIM_R                                      ~47     !8, 'header'
         58        FETCH_DIM_W                                      $44     !10, !6
         59        FETCH_DIM_W                                      $45     $44, 'header'
         60        ASSIGN_DIM                                               $45
         61        OP_DATA                                                  ~47
   73    62        INIT_FCALL                                               'array_unique'
         63        FETCH_DIM_R                                      ~50     !10, !6
         64        FETCH_DIM_R                                      ~51     ~50, 'header'
         65        SEND_VAL                                                 ~51
         66        DO_ICALL                                         $52     
         67        FETCH_DIM_W                                      $48     !10, !6
         68        ASSIGN_DIM                                               $48, 'header'
         69        OP_DATA                                                  $52
   76    70        INIT_FCALL                                               'array_merge'
         71        FETCH_DIM_R                                      ~55     !10, !6
         72        FETCH_DIM_R                                      ~56     ~55, 'keywords'
         73        SEND_VAL                                                 ~56
         74        FETCH_DIM_R                                      ~57     !5, 'keywords'
         75        SEND_VAL                                                 ~57
         76        FETCH_DIM_R                                      ~58     !8, 'keywords'
         77        SEND_VAL                                                 ~58
         78        DO_ICALL                                         $59     
         79        FETCH_DIM_W                                      $53     !10, !6
         80        ASSIGN_DIM                                               $53, 'keywords'
         81        OP_DATA                                                  $59
   77    82        INIT_FCALL                                               'array_unique'
         83        FETCH_DIM_R                                      ~62     !10, !6
         84        FETCH_DIM_R                                      ~63     ~62, 'keywords'
         85        SEND_VAL                                                 ~63
         86        DO_ICALL                                         $64     
         87        FETCH_DIM_W                                      $60     !10, !6
         88        ASSIGN_DIM                                               $60, 'keywords'
         89        OP_DATA                                                  $64
   80    90        ASSIGN_DIM                                               !4
         91        OP_DATA                                                  !9
   56    92    > > JMP                                                      ->25
         93    >   FE_FREE                                                  $26
   53    94      > JMP                                                      ->14
         95    >   FE_FREE                                                  $18
   86    96        INIT_FCALL                                               'var_dump'
         97        SEND_VAR                                                 !10
         98        DO_ICALL                                                 
         99      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
161.88 ms | 1025 KiB | 19 Q