Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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." | ";
    }
 }
share|improve this question
    
array_diff can handle arrays of different lengths just fine. –  deceze Jan 9 at 9:36
    
sir if you run my code you will see that array_diff ignored the last value of Array2 which is fox. May I ask if what part of my code could have been the reason for this if not array_diff? –  SOS Jan 9 at 9:38
    
What you may be missing is that array_diff "returns an array containing all the entries from array1 that are not present in any of the other arrays." php.net/array_diff –  deceze Jan 9 at 9:39
    
so what your saying is if I switch the value of array1 to those of array2 the value fox will be considered as difference? –  SOS Jan 9 at 9:42
    
Yes. The "total" difference of both arrays is array_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

3 Answers 3

up vote 1 down vote accepted

Try this

$str1 = "The quisck browsn";
$str2 = "The quick brown fox";
$tempArr;
$var2;
$ctr=0;

$strarr = (explode(" ",$str1));
echo("<br>");

$strarr2 = (explode(" ",$str2));
echo("<br>");

if(sizeof($strarr) > sizeof($strarr2)){
    $result = array_diff($strarr,$strarr2);
}else{
    $result = array_diff($strarr2,$strarr);
}

The above will return the difference between array size greater than the lower.i.e. element present in first array but not in 2nd.

But if you want the complete difference between 2 of them i.e. element in first array does not exist in 2nd and vice versa you can do something as

$fullDiff = array_merge(array_diff($strarr, $strarr2), array_diff($strarr2, $strarr));
share|improve this answer
    
thnx mate I'll give it a shot –  SOS Jan 9 at 9:45
    
I have just updated the answer check now. –  Abhik Chakraborty Jan 9 at 9:49
$str1 = "The quisck browsn";
$str2 = "The quick brown fox";
$tempArr;
$var2;
$ctr=0;

echo "Array1:<br> {$str1} <br><br>Array2:<br> {$str2}";

$strarr = (explode(" ",$str2));
echo("<br>");

$strarr2 = (explode(" ",$str1));
echo("<br>");

$result = array_diff($strarr,$strarr2);
//print_r($result);

if (count($result) > 0){
    echo "<br>Differences: | " ;
    foreach ($result AS $result){
        echo $result." | ";
    }
}   

Use this since it retruns an array containing all the entries from $str2 that are not present in any of the other arrays.

share|improve this answer

if your read the documentation of array_diff(). It say's that

   array_diff(array1,array2,array3...);

This function compares the values of two (or more) arrays, and return an array that contains the entries from array1 that are not present in array2 or array3, etc.

You can first find the array with more element by using a count function

and then pass the argument as below

     <?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>");

      /*
       Check Which array is bigger 
      */
      if(count($strarr)>count()$strarr2 ){

      $result = array_diff($strarr,$strarr2);

      }else{

       $result = array_diff($strarr2,$strarr);

      }
     if (count($result) > 0){
        echo "<br>Differences: | " ;
        foreach ($result AS $result){
        echo $result." | ";
     }
      }
   ?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.