3v4l.org

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

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
224.11 ms | 2641 KiB | 17 Q