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") ) ) ); $new =[]; foreach($array as $subarr){ $new = array_merge($new, $subarr); } var_dump($new); foreach($new as $key => $story){ $words = "/" . implode("|", $story["keywords"]) . "/i"; foreach($new as $key2 => $story2){ if($key != $key2 && $key2 > $key && !in_array($key2, $merged)){ if(count(preg_grep($words, $story2["keywords"])) >1){ echo $key . " " . $key2 . "\n"; echo $story["header"] . " = " . $story2["header"] ."\n\n"; $res[$key] = ["header" => [$story["header"], $story2["header"]], "keywords" => array_merge($story["keywords"], $story2["keywords"])]; $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 = 15, Position 2 = 77
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 77
Branch analysis from position: 16
2 jumps found. (Code = 77) Position 1 = 26, Position 2 = 75
Branch analysis from position: 26
2 jumps found. (Code = 78) Position 1 = 27, Position 2 = 75
Branch analysis from position: 27
2 jumps found. (Code = 46) Position 1 = 30, Position 2 = 32
Branch analysis from position: 30
2 jumps found. (Code = 46) Position 1 = 33, Position 2 = 39
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 40, Position 2 = 74
Branch analysis from position: 40
2 jumps found. (Code = 43) Position 1 = 48, Position 2 = 74
Branch analysis from position: 48
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
Branch analysis from position: 74
Branch analysis from position: 74
Branch analysis from position: 39
Branch analysis from position: 32
Branch analysis from position: 75
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 75
Branch analysis from position: 77
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 77
Branch analysis from position: 10
filename:       /in/MYsWR
function name:  (null)
number of ops:  79
compiled vars:  !0 = $array, !1 = $new, !2 = $subarr, !3 = $story, !4 = $key, !5 = $words, !6 = $story2, !7 = $key2, !8 = $merged, !9 = $res
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   45     1        ASSIGN                                                   !1, <array>
   46     2      > FE_RESET_R                                       $12     !0, ->10
          3    > > FE_FETCH_R                                               $12, !2, ->10
   47     4    >   INIT_FCALL                                               'array_merge'
          5        SEND_VAR                                                 !1
          6        SEND_VAR                                                 !2
          7        DO_ICALL                                         $13     
          8        ASSIGN                                                   !1, $13
   46     9      > JMP                                                      ->3
         10    >   FE_FREE                                                  $12
   49    11        INIT_FCALL                                               'var_dump'
         12        SEND_VAR                                                 !1
         13        DO_ICALL                                                 
   50    14      > FE_RESET_R                                       $16     !1, ->77
         15    > > FE_FETCH_R                                       ~17     $16, !3, ->77
         16    >   ASSIGN                                                   !4, ~17
   51    17        INIT_FCALL                                               'implode'
         18        SEND_VAL                                                 '%7C'
         19        FETCH_DIM_R                                      ~19     !3, 'keywords'
         20        SEND_VAL                                                 ~19
         21        DO_ICALL                                         $20     
         22        CONCAT                                           ~21     '%2F', $20
         23        CONCAT                                           ~22     ~21, '%2Fi'
         24        ASSIGN                                                   !5, ~22
   52    25      > FE_RESET_R                                       $24     !1, ->75
         26    > > FE_FETCH_R                                       ~25     $24, !6, ->75
         27    >   ASSIGN                                                   !7, ~25
   53    28        IS_NOT_EQUAL                                     ~27     !4, !7
         29      > JMPZ_EX                                          ~27     ~27, ->32
         30    >   IS_SMALLER                                       ~28     !4, !7
         31        BOOL                                             ~27     ~28
         32    > > JMPZ_EX                                          ~27     ~27, ->39
         33    >   INIT_FCALL                                               'in_array'
         34        SEND_VAR                                                 !7
         35        SEND_VAR                                                 !8
         36        DO_ICALL                                         $29     
         37        BOOL_NOT                                         ~30     $29
         38        BOOL                                             ~27     ~30
         39    > > JMPZ                                                     ~27, ->74
   54    40    >   INIT_FCALL                                               'preg_grep'
         41        SEND_VAR                                                 !5
         42        FETCH_DIM_R                                      ~31     !6, 'keywords'
         43        SEND_VAL                                                 ~31
         44        DO_ICALL                                         $32     
         45        COUNT                                            ~33     $32
         46        IS_SMALLER                                               1, ~33
         47      > JMPZ                                                     ~34, ->74
   55    48    >   CONCAT                                           ~35     !4, '+'
         49        CONCAT                                           ~36     ~35, !7
         50        CONCAT                                           ~37     ~36, '%0A'
         51        ECHO                                                     ~37
   56    52        FETCH_DIM_R                                      ~38     !3, 'header'
         53        CONCAT                                           ~39     ~38, '+%3D+'
         54        FETCH_DIM_R                                      ~40     !6, 'header'
         55        CONCAT                                           ~41     ~39, ~40
         56        CONCAT                                           ~42     ~41, '%0A%0A'
         57        ECHO                                                     ~42
   57    58        FETCH_DIM_R                                      ~44     !3, 'header'
         59        INIT_ARRAY                                       ~45     ~44
         60        FETCH_DIM_R                                      ~46     !6, 'header'
         61        ADD_ARRAY_ELEMENT                                ~45     ~46
         62        INIT_ARRAY                                       ~47     ~45, 'header'
         63        INIT_FCALL                                               'array_merge'
         64        FETCH_DIM_R                                      ~48     !3, 'keywords'
         65        SEND_VAL                                                 ~48
         66        FETCH_DIM_R                                      ~49     !6, 'keywords'
         67        SEND_VAL                                                 ~49
         68        DO_ICALL                                         $50     
         69        ADD_ARRAY_ELEMENT                                ~47     $50, 'keywords'
         70        ASSIGN_DIM                                               !9, !4
         71        OP_DATA                                                  ~47
   58    72        ASSIGN_DIM                                               !8
         73        OP_DATA                                                  !7
   52    74    > > JMP                                                      ->26
         75    >   FE_FREE                                                  $24
   50    76      > JMP                                                      ->15
         77    >   FE_FREE                                                  $16
   64    78      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.72 ms | 1404 KiB | 23 Q