<?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);
array(6) {
[0]=>
array(2) {
["header"]=>
string(31) "This is a story about a monkey."
["keywords"]=>
array(9) {
[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"
}
}
[1]=>
array(2) {
["header"]=>
string(36) "Here's a catarific story about a cat"
["keywords"]=>
array(8) {
[0]=>
string(5) "meauw"
[1]=>
string(5) "raaaw"
[2]=>
string(5) "kitty"
[3]=>
string(6) "growup"
[4]=>
string(3) "Fun"
[5]=>
string(5) "claws"
[6]=>
string(4) "fish"
[7]=>
string(6) "salmon"
}
}
[2]=>
array(2) {
["header"]=>
string(53) "This is another, but different story, about a monkey."
["keywords"]=>
array(8) {
[0]=>
string(6) "Monkey"
[1]=>
string(3) "Big"
[2]=>
string(5) "Trees"
[3]=>
string(6) "Bonobo"
[4]=>
string(3) "Fun"
[5]=>
string(5) "Dance"
[6]=>
string(3) "Cow"
[7]=>
string(8) "Coconuts"
}
}
[3]=>
array(2) {
["header"]=>
string(36) "Here's a different story about a cat"
["keywords"]=>
array(8) {
[0]=>
string(6) "meauwe"
[1]=>
string(4) "ball"
[2]=>
string(3) "cat"
[3]=>
string(6) "kitten"
[4]=>
string(5) "claws"
[5]=>
string(5) "sleep"
[6]=>
string(4) "fish"
[7]=>
string(4) "purr"
}
}
[4]=>
array(2) {
["header"]=>
string(37) "This is a third story about a monkey."
["keywords"]=>
array(9) {
[0]=>
string(6) "Jungle"
[1]=>
string(4) "tree"
[2]=>
string(6) "monkey"
[3]=>
string(6) "Bonobo"
[4]=>
string(3) "Fun"
[5]=>
string(5) "Dance"
[6]=>
string(8) "climbing"
[7]=>
string(7) "Coconut"
[8]=>
string(6) "pretty"
}
}
[5]=>
array(2) {
["header"]=>
string(31) "This is a story about a fireman"
["keywords"]=>
array(8) {
[0]=>
string(4) "fire"
[1]=>
string(9) "explosion"
[2]=>
string(7) "burning"
[3]=>
string(6) "rescue"
[4]=>
string(5) "happy"
[5]=>
string(4) "help"
[6]=>
string(5) "water"
[7]=>
string(3) "car"
}
}
}
Notice: Undefined variable: merged in /in/MYsWR on line 53
Warning: in_array() expects parameter 2 to be array, null given in /in/MYsWR on line 53
Notice: Undefined variable: merged in /in/MYsWR on line 53
Warning: in_array() expects parameter 2 to be array, null given in /in/MYsWR on line 53
0 2
This is a story about a monkey. = This is another, but different story, about a monkey.
0 4
This is a story about a monkey. = This is a third story about a monkey.
1 3
Here's a catarific story about a cat = Here's a different story about a cat
Output for 7.3.32 - 7.3.33
array(6) {
[0]=>
array(2) {
["header"]=>
string(31) "This is a story about a monkey."
["keywords"]=>
array(9) {
[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"
}
}
[1]=>
array(2) {
["header"]=>
string(36) "Here's a catarific story about a cat"
["keywords"]=>
array(8) {
[0]=>
string(5) "meauw"
[1]=>
string(5) "raaaw"
[2]=>
string(5) "kitty"
[3]=>
string(6) "growup"
[4]=>
string(3) "Fun"
[5]=>
string(5) "claws"
[6]=>
string(4) "fish"
[7]=>
string(6) "salmon"
}
}
[2]=>
array(2) {
["header"]=>
string(53) "This is another, but different story, about a monkey."
["keywords"]=>
array(8) {
[0]=>
string(6) "Monkey"
[1]=>
string(3) "Big"
[2]=>
string(5) "Trees"
[3]=>
string(6) "Bonobo"
[4]=>
string(3) "Fun"
[5]=>
string(5) "Dance"
[6]=>
string(3) "Cow"
[7]=>
string(8) "Coconuts"
}
}
[3]=>
array(2) {
["header"]=>
string(36) "Here's a different story about a cat"
["keywords"]=>
array(8) {
[0]=>
string(6) "meauwe"
[1]=>
string(4) "ball"
[2]=>
string(3) "cat"
[3]=>
string(6) "kitten"
[4]=>
string(5) "claws"
[5]=>
string(5) "sleep"
[6]=>
string(4) "fish"
[7]=>
string(4) "purr"
}
}
[4]=>
array(2) {
["header"]=>
string(37) "This is a third story about a monkey."
["keywords"]=>
array(9) {
[0]=>
string(6) "Jungle"
[1]=>
string(4) "tree"
[2]=>
string(6) "monkey"
[3]=>
string(6) "Bonobo"
[4]=>
string(3) "Fun"
[5]=>
string(5) "Dance"
[6]=>
string(8) "climbing"
[7]=>
string(7) "Coconut"
[8]=>
string(6) "pretty"
}
}
[5]=>
array(2) {
["header"]=>
string(31) "This is a story about a fireman"
["keywords"]=>
array(8) {
[0]=>
string(4) "fire"
[1]=>
string(9) "explosion"
[2]=>
string(7) "burning"
[3]=>
string(6) "rescue"
[4]=>
string(5) "happy"
[5]=>
string(4) "help"
[6]=>
string(5) "water"
[7]=>
string(3) "car"
}
}
}
Warning: in_array() expects parameter 2 to be array, null given in /in/MYsWR on line 53
Warning: in_array() expects parameter 2 to be array, null given in /in/MYsWR on line 53
0 2
This is a story about a monkey. = This is another, but different story, about a monkey.
0 4
This is a story about a monkey. = This is a third story about a monkey.
1 3
Here's a catarific story about a cat = Here's a different story about a cat