I used array_diff to compare to 2 strings converted into arrays by explode, it can compare 2 arrays of the same length, how I accomplish comparing arrays of different length?
Ex.
Array1: The quisck browsn fosx
Array2: The quick brown fox
Works!!
Array1: The quisck browsn
Array2: The quick brown fox
Doesn't Work!!(fox was not mentioned)
<?php
$str1 = "The quisck browsn";
$str2 = "The quick brown fox";
$tempArr;
$var2;
$ctr=0;
echo "Array1:<br> $str1 <br><br>Array2:<br> $str2";
$strarr = (explode(" ",$str1));
echo("<br>");
$strarr2 = (explode(" ",$str2));
echo("<br>");
$result = array_diff($strarr,$strarr2);
//print_r($result);
if (count($result) > 0){
echo "<br>Differences: | " ;
foreach ($result AS $result){
echo $result." | ";
}
}
array_diff
can handle arrays of different lengths just fine. – deceze Jan 9 at 9:36array_diff
"returns an array containing all the entries fromarray1
that are not present in any of the other arrays." php.net/array_diff – deceze Jan 9 at 9:39array_merge(array_diff($a, $b), array_diff($b, $a))
. Or alternatively, take the intersection of both arrays and remove it from the union of both (array_diff(array_merge($a, $b), array_intersect($a, $b))
). – deceze Jan 9 at 9:46