3

I have arrays like these :

$array1 = [1,2,3]; 

$array2 = [3,2,1]; 
$array3 = [2,1,3]; 
$array4 = [2,1,3]; 
$array5 = [1,1,1]; 
$array6 = [3,3,2]; 
$array7 = [1,2,1];
$array8 = [8,9,2]; 

I want to check how array2 until array8 compare to array1. It should give me expected return like this :

$array2 = [3,2,1]; return 'match'
$array3 = [2,1,3]; return 'match'
$array4 = [2,3,1]; return 'match'
$array5 = [1,1,1]; return 'not match'
$array6 = [3,3,2]; return 'not match'
$array7 = [1,2,1]; return 'not match'
$array8 = [8,9,2]; return 'not match'

I tried to compare it using array_diff() but sometimes the result is not like what I expected, especially if on array2 have two same values.

note : array2 until array8 need to always have all 3 values from array1

1
  • 1
    Post the code you have tried using the array_diff. Commented Apr 1, 2019 at 12:25

6 Answers 6

5

You just need to sort both arrays before comparing them e.g.

sort($array1);
for ($i = 2; $i <= 8; $i++) {
    sort(${"array$i"});
    echo "array $i: " . ($array1 == ${"array$i"} ? 'match' : 'no match') . "\n";
}

Output:

array 2: match 
array 3: match 
array 4: match 
array 5: no match 
array 6: no match 
array 7: no match 
array 8: no match

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the quick respond sir but if like this $array1 = [7,6,7]; $array2 = [4,7,6]; sort($array1); sort($array2); echo "array " . (empty(array_diff($array1, $array2)) ? 'match' : 'no match') . "\n"; if like that it will return 'array match' when it shouldn't
wow this actually working, may i know the why the first one not working and now this working ? is the magic is on the sorting ?
The problem with the first code is that array_diff only looks to see if all the values in $array1 are in $array2. In your test case the values in $array1 were 6 and 7, both of which are in $array2 so array_diff returned an empty array which looked like a match. My new code uses an exact comparison, so the arrays have to have the same number of values and each has to be equal to the corresponding value in the other array. See php.net/manual/en/language.operators.array.php
4

You can use array_unique() and then array_diff() for this task:

$array1 = array_unique($array1);
$array2 = array_unique($array2);
$result = array_diff($array1, $array2);

Description of array_unique():

array_unique ( array $array [, int $sort_flags = SORT_STRING ] ) : array

array_unique — Removes duplicate values from an array

Sorting type flags:

  • SORT_REGULAR - compare items normally (don't change types)
  • SORT_NUMERIC - compare items numerically
  • SORT_STRING - compare items as strings
  • SORT_LOCALE_STRING - compare items as strings, based on the current locale.

Comments

1

You need to sort the arrays so that indexing will be comparable.

Take a parent array and add all array elements in it.

Loop over the parent array.

sort() children arrays.

Compare there and you will get results.

Code:

<?php
$array1 = [1,2,3]; 
$array2 = [3,2,1]; 
$array3 = [2,1,3]; 
$array4 = [2,1,3]; 
$array5 = [1,1,1]; 
$array6 = [3,3,2]; 
$array7 = [1,2,1];
$array8 = [8,9,2];


$arr['array_1'] = [1,2,3];
$arr['array_2'] = [3,2,1]; 
$arr['array_3'] = [2,1,3]; 
$arr['array_4'] = [2,1,3]; 
$arr['array_5'] = [1,1,1];
$arr['array_6'] = [3,3,2]; 
$arr['array_7'] = [1,2,1];
$arr['array_8'] = [8,9,2];

$find = [1,2,3];
if (! empty($arr)) {
 foreach ($arr as $key => $elem) {
  sort($arr[$key]);
  if ($arr[$key] == $find) {
   echo "<br/>[" . implode(',', $elem) . "]: Match";
  }
  else {
   echo "<br/>[" . implode(',', $elem) . "]: No Match";
  }
 }
}

Output:

[1,2,3]: Match
[3,2,1]: Match
[2,1,3]: Match
[2,1,3]: Match
[1,1,1]: No Match
[3,3,2]: No Match
[1,2,1]: No Match
[8,9,2]: No Match

Comments

1

Although using array_diff is okay, there's an easier way to compare two arrays you can just use ==

<?php

$array1 = [1,2,3]; 

$array2 = [3,2,1]; 
$array3 = [2,1,3]; 
$array4 = [2,1,3]; 
$array5 = [1,1,1]; 
$array6 = [3,3,2]; 
$array7 = [1,2,1];
$array8 = [8,9,2]; 


for($i = 2; $i <= 8; ++$i) {
    sort(${"array$i"});
    echo ${"array$i"} == $array1 ? 'match' : 'not match';
    echo PHP_EOL;
}

output:

match
match
match
not match
not match
not match
not match

PHP MANUAL

$a == $b Equality TRUE if $a and $b have the same key/value pairs.

$a === $b Identity TRUE if $a and $b have the same key/value pairs in the same order and of the same types.

Comments

0

The fastest way would be to convert your array to associative one. You can use array_flip function to achieve it. Then you need only to check difference in keys which should be fast using array_diff_key. For example:

$array1 = [1,2,3]; 
$array2 = [3,2,1]; 
$array3 = [2,1,3]; 
$array4 = [2,1,3]; 
$array5 = [1,1,1]; 
$array6 = [3,3,2]; 
$array7 = [1,2,1];
$array8 = [8,9,2]; 

var_dump(
  empty( // if it's empty then the arrays are identical
    array_diff_key(array_flip($array1), array_flip($array5))
  )
);

But this will only work if you don't care if any array contains duplicate values. If number or order of elements is important then this won't work.

Comments

0

at first need to reduce duplicate data from each array by using php array_unique function Then make difference two array by using array_diff function

<?php
    $firstArray = array_unique($firstArray);
    $secondArray = array_unique($secondArray);
    $arrayDiff = array_diff($firstArray, $secondArray);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.