3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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);
based on 6hgLu
Output for hhvm-3.18.5 - 3.22.0, 7.2.0 - 7.3.0alpha1
array(1) { [0]=> array(2) { ["header"]=> array(3) { [0]=> string(31) "This is a story about a monkey." [1]=> string(53) "This is another, but different story, about a monkey." [3]=> string(37) "This is a third story about a monkey." } ["keywords"]=> array(18) { [0]=> string(5) "Trees" [1]=> string(6) "Monkey" [2]=> string(6) "Flying" [3]=> string(5) "Drink" [4]=> string(8) "Vacation" [5]=> string(7) "Coconut" [6]=> string(3) "Big" [7]=> string(6) "Bonobo" [8]=> string(8) "Climbing" [9]=> string(3) "Fun" [10]=> string(5) "Dance" [11]=> string(3) "Cow" [12]=> string(8) "Coconuts" [13]=> string(6) "Jungle" [14]=> string(4) "tree" [15]=> string(6) "monkey" [16]=> string(8) "climbing" [17]=> string(6) "pretty" } } }
Output for 5.6.30, 7.0.30 - 7.1.17
array(1) { [0]=> array(2) { ["header"]=> array(3) { [0]=> string(31) "This is a story about a monkey." [1]=> string(53) "This is another, but different story, about a monkey." [9]=> string(37) "This is a third story about a monkey." } ["keywords"]=> array(18) { [0]=> string(5) "Trees" [1]=> string(6) "Monkey" [2]=> string(6) "Flying" [3]=> string(5) "Drink" [4]=> string(8) "Vacation" [5]=> string(7) "Coconut" [6]=> string(3) "Big" [7]=> string(6) "Bonobo" [8]=> string(8) "Climbing" [9]=> string(3) "Fun" [10]=> string(5) "Dance" [11]=> string(3) "Cow" [12]=> string(8) "Coconuts" [13]=> string(6) "Jungle" [14]=> string(4) "tree" [15]=> string(6) "monkey" [16]=> string(8) "climbing" [17]=> string(6) "pretty" } } }