begineeeerrrr
begineeeerrrr

Reputation: 343

Laravel, how to find duplicated value between 2 arrays with different keys?

I have 3 arrays, how do I return 'true' if the 2 array have the exact same value? In this case, $array1 and $array2 is true meanwhile $array1 and $array3 is 'false' because one of the value is not same. Is there an eloquent way to do this?

$array1 = array('dog'=>1, 'cat'=>2, 'monkey'=>3);
$array2 = array('red'=>1, 'green'=>2, 'blue'=>3);
$array3 = array('desk'=>1, 'chair'=>2, 'carpet'=>2);

Upvotes: 1

Views: 2034

Answers (6)

Milan Chheda
Milan Chheda

Reputation: 8249

You should see Array operators:

$arraysAreEqual = ($a == $b); // TRUE if $a and $b have the same key/value pairs.
$arraysAreEqual = ($a === $b); // TRUE if $a and $b have the same key/value pairs in the same order and of the same types.

The inequality operator is != while the non-identity operator is !== to match the equality operator == and the identity operator ===.


As an alternative, you can use implode() and check if the strings are equal, method 2:

if(implode('', $array1) === implode('', $array2)) {
    echo "Equal";
}

EDIT BY OP the final answer

foreach($array1 as $key => $value) 
{ 
  foreach($array2 as $key1 => $value1) 
  { 
    if(implode('', $array1[$key]) === implode('', $array2[$key1])) 
    { 
      echo "EQUAL"; 
    } 
  } 
}

Upvotes: 1

Don't Panic
Don't Panic

Reputation: 14520

@mimo's solution is a great way to do it with plain arrays. However you explicitly asked for a Laravel solution, so here you go:

function areSame($a1, $a2) {
    return collect($a1)->diff(collect($a2))->count() === 0;
}

This simply creates collections of the input arrays, so we can use Laravel's collection methods on them. Then find the differences between the input collections - if there are differences, this will result in a collection of X items. If there are no differences, the resulting diff collection will be empty.

areSame($array1, $array2) // TRUE
areSame($array1, $array3) // FALSE

Upvotes: 1

Ali Faris
Ali Faris

Reputation: 18592

checkout this :

function compare($arr1 , $arr2)
{
  return array_values($arr1) == array_values($arr2); 
}

$anyTowArraysAreEquals = compare($array1 , $array2) || compare($array1 , $array3) || compare($array2 , $array3);

Upvotes: 0

Alex
Alex

Reputation: 2775

You can use array_diff function. Like this:

$array1 = array('dog'=>1, 'cat'=>2, 'monkey'=>3);
$array2 = array('red'=>1, 'green'=>2, 'blue'=>3);
$array3 = array('desk'=>1, 'chair'=>2, 'carpet'=>2);

if (count(array_diff($array1, $array2)) > 0) {
    echo 'false';
} else {
    echo 'true';
}

And in this example $array1 and $array2 is true; $array1 and $array3 is false.

Upvotes: 0

cre8
cre8

Reputation: 13562

function compare($arrayX, $arrayY) {
    return array_values($arrayX) == array_values($arrayY)
}

compare($array1, $array2) // TRUE
compare($array1, $array3) // FALSE

array_values will return you all values of the array in an array.

Upvotes: 3

Daniel Bejan
Daniel Bejan

Reputation: 1468

You could try extracting the values from the arrays using array_values and then comparing them like

$array1 = array('dog'=>1, 'cat'=>2, 'monkey'=>3);
$array1v = array_values($array1);
$array2 = array('red'=>1, 'green'=>2, 'blue'=>3);
$array2v = array_values($array2);
$array3 = array('desk'=>1, 'chair'=>2, 'carpet'=>2);
$array3v = array_values($array3);

and then

$array1v == $array2v; //true
$array1v == $array3v; //false

Upvotes: 0

Related Questions