You code is overly complicated ... and that's why you go to infinite loops...
Here is a suggestion with foreach loop, you don't need to care how long you arrays are in this example, since you are checking if the words that come from the array you are looping through appear in the other... so you are already checking all of the words in the array.
This means that if you use something like in_array() you will know if that word appear in the other array or not.
So here is a small change to your code:
<?php
function show_unique_strings($array1, $array2) {
$array1 = explode(" ", $array1);
$array2 = explode(" ", $array2);
foreach ($array2 as $a2Val){
$nKey = in_array($a2Val,$array1);
if($nKey > 0){
echo "<b> $a2Val </b>";
} else {
echo " $a2Val";
}
}
}
$string1 = "TO THE ORDER OF United Bank Limited Arab -09254 DT:17-06-20212 ADS";
$string2 = "Arab TO THE ORDER OF United Bank Limited Arab TO -092541 KDS DT:17-06-20212";
echo $string1.'<br/>';
show_unique_strings($string1,$string2);
?>
Will output:
TO THE ORDER OF United Bank Limited Arab -09254 DT:17-06-20212 ADS
Arab TO THE ORDER OF United Bank Limited Arab TO -092541 KDS DT:17-06-20212
This way we don't care if we run only on the 2nd array because for each word we do the check and echo one or the other... no need of counts and following our keys at all...:)
simplediff library did my job, I just had to modify code to meet my requirement.
<?php
function diff($old, $new){
$matrix = array();
$maxlen = 0;
foreach($old as $oindex => $ovalue){
$nkeys = array_keys($new, $ovalue);
foreach($nkeys as $nindex){
$matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
$matrix[$oindex - 1][$nindex - 1] + 1 : 1;
if($matrix[$oindex][$nindex] > $maxlen){
$maxlen = $matrix[$oindex][$nindex];
$omax = $oindex + 1 - $maxlen;
$nmax = $nindex + 1 - $maxlen;
}
}
}
if($maxlen == 0) return array(array('d'=>$old, 'i'=>$new));
return array_merge(
diff(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
array_slice($new, $nmax, $maxlen),
diff(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen)));
}
$string1 = "TO THE ORDER OF United Bank Limited Arab -09254 DT:17-06-20212 ADS";
$string2 = "Arab TO THE ORDER OF United Bank Limited Arab TO -092541 KDS DT:17-06-20212";
$arr=diff(explode(" ",$string1),explode(" ",$string2));
echo $string1.'<br/>';
$ret = '';
foreach($arr as $k){
if(is_array($k)){
$ret .= (!empty($k['i'])?"<b>".implode(' ',$k['i'])."</b> ":'');
}
else {
$ret .= $k . ' ';
}
}
echo $ret;
?>
output:
TO THE ORDER OF United Bank Limited Arab -09254 DT:17-06-20212 ADS
Arab TO THE ORDER OF United Bank Limited Arab TO -092541 KDS DT:17-06-20212
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.