Я пытаюсь запрограммировать скрипт на php, который находит похожие фразы в двух частях текста. Который я задал этот Вопрос
1. the cat is on the roof
2. a man is on the stage
A1 = [the, cat, is, on, the, roof]
A2 = [a, man, is, on, the, stage]
[the]: no match
[cat]: no match
[is]: match
[is, on]: match
[is, on, the]: match
[is, on, the, roof]: no match
[on]: match
[on, the]: match
[on, the, roof]: no match
[the]: match
[the, roof]: no match
[roof]: no match
-end-
Мой код указан ниже.
<?php
echo match(explode(' ',$text1), explode(' ',$text2));
function match($old, $new){
$arr;
foreach($old as $key=>$oldword) {
foreach($new as $key2=>$newword) {
if($old[$key] == $new[$key2]) {
array_push($arr,$old[$key]);
echo '<span style="color:red;">'.$old[$key].' </span>';
}
else {
echo $old[$key].' ';
}
}
}
}
Я получаю следующий вывод

Почему слова повторяются?