1

I have two arrays, each generated from a string. The strings are:

$string1 = "#574390, #574387, #574386, #574383 (keyboard enter)
#574368, #574367, #574364, #574361, #574357, #574355, #574351, #574343, #574341 (keyboard enter)
#574381, #574379, #574377, #574375, #574374, #574373, #574372, #574371, #574369"

$string2 = "574390
574386
574383
574381
574379
574377
574375
574374
574373
574372
574371
574369
574368
574367
574364
574361
574357
574355
574351
574343
574341"

Then, I do this to explode each string to array:

$str1 = checkstring($string1);
$str2 = checkstring($string2);

function checkstring($x) {
//check whether the string has "#" in it
   if (!strstr($x, '#')) {
     $array1 = str_replace(" ", "", $x);
     $array1 = explode("\n", str_replace("\r", "", $array1));
     return $array1;
   }
   else {
      $array2 = str_replace("\r", ", ", str_replace("#", '', $x));
      $array2 = array_unique(explode(", ", $array2), SORT_REGULAR);
      return $array2;
   }
}

After that, I try to find the difference between the two array:

$result = array_diff($str1, $str2);
print_r($result);

As you can see, the difference between array 1 and array 2 is that in array 1 there is 574387 but not in array 2. The result that I get from the code is this:

Array ( [1] => 574387 [4] => 574368 [13] => 574381 )

And if I switch the value between $string1 and $string2 the result will be this:

Array ( [3] => 574381 [12] => 574368 )

I do the switching because I want to make it able to check both ways. I was wondering what's wrong with it. Thanks for the help.

rhog23
  • 311
  • 2
  • 16
  • `array_diff() returns the values in array1 that are not present in any of the other arrays.` so when you switch, it checkes that which value of second-array not exist in first-one and that's why anser differs – Alive to die - Anant Jan 09 '18 at 04:37
  • Do NOT use `strstr()` to check for the existence of a substring. The PHP manual makes this point specifically: http://php.net/manual/en/function.strstr.php Also, if this was my project, I'd be using regex to generate the array of numbers for `array1`. – mickmackusa Jan 09 '18 at 04:43
  • Sorry, I generally downvote questions where the answer is to copy-paste the function definition into an answer. This means you didn't read the manual before posting your question. – mickmackusa Jan 09 '18 at 04:46

2 Answers2

6

[array_diff] Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays.

Taken from php.net

So, it only shows the elements of array1 (in your case $str1), which are different to the compared arrays (in your case $str2). That is why 574387 is not shown in the second comparison. Also, the other two differences are shown, because instead of using a comma, you use a (keyboard enter) in front of them in $str1.

If you want to see the differences from both arrays, try something like this:

array_merge(array_diff($str1,$str2),array_diff($str2,$str1));
Geshode
  • 3,600
  • 6
  • 18
  • 32
0

You can try this

preg_match_all('/[0-9]+/', $string1, $result);
$array1 = array_unique($result[0]);
preg_match_all('/[0-9]+/', $string2, $result);
$array2 = array_unique($result[0]);
$arrayDiff = array_merge(array_diff($array1, $array2), array_diff($array2, $array1));
print_r($arrayDiff);
Vũ Nhật Anh
  • 512
  • 5
  • 14