1

I have 2 arrrays, And i need return TRUE or FALSE when 2 arrays matched each other. also un-ordered arrays should return TRUE if results matched, but only should return true if both arrays have same values.

//This should return TRUE
$array_One = array('test1', 'test2', 'test3');
$array_Two = array('test1', 'test2', 'test3');

//This should return TRUE
$array_One = array('test1', 'test2', 'test3');
$array_Two = array('test1', 'test3', 'test2');

//This should return TRUE
$array_One = array('test1', 'test2', 'test3');
$array_Two = array('test1', 'test3', 'test2');

//This should return FALSE
$array_One = array('test1', 'test2', 'test3');
$array_Two = array('test1', 'test2');

I tried few methods, including array_key_exists by using foreach, But it does not returned the expected result. This should return ONLY one TRUE or FALSE when arrays matched.

7
  • 8
    Have you tried array_diff()? Commented Dec 10, 2012 at 13:33
  • 1
    The second codeblock, the array values, doesn't match, but you say it should be returning true, is what you want just comparing the sizes of the arrays? Commented Dec 10, 2012 at 13:34
  • Agreed with JamWaffles. Do an array_diff and see if the resulting array is empty or not. Commented Dec 10, 2012 at 13:34
  • @JamWaffles - you should put that in as an answer. Commented Dec 10, 2012 at 13:35
  • @JamWaffles : array_diff return the difference of arrays, So how can I return just only a one TRUE or false ? Commented Dec 10, 2012 at 13:36

5 Answers 5

3

http://php.net/manual/en/function.array-diff.php

If you just need to know if two arrays' values are exactly the same (regardless of keys and order), then instead of using array_diff, this is a simple method:

<?php

function identical_values( $arrayA , $arrayB ) {

    sort( $arrayA );
    sort( $arrayB );

    return $arrayA == $arrayB;
}

// Examples:

$array1 = array( "red" , "green" , "blue" );
$array2 = array( "green" , "red" , "blue" );
$array3 = array( "red" , "green" , "blue" , "yellow" );
$array4 = array( "red" , "yellow" , "blue" );
$array5 = array( "x" => "red" , "y" =>  "green" , "z" => "blue" );

identical_values( $array1 , $array2 );  // true
identical_values( $array1 , $array3 );  // false
identical_values( $array1 , $array4 );  // false
identical_values( $array1 , $array5 );  // true

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

Comments

1
$array_One = array('test1', 'test2', 'test3');
$array_Two = array('test1', 'test3', 'test2');

if (array_diff($array_One, $array_Two))
{
    // there is a difference
    echo 'FALSE';
}
else
{
    // the arrays match
    echo 'TRUE';
}

Comments

1

If you want to compare the sizes of the arrays, what you can do is use count:

if ( count ( $array1 ) == count ( $array2 )) 
{ 
     MATCH! 
} 
else 
{ 
     NO MATCH! 
}

Comments

1

function arraycomp( $array1, $array2 ) {
   $diff1 = array_values( $array1 );
   $diff2 = array_values( $array2 );

   sort( $diff1 );
   sort( $diff2 );

   return ( $diff1 === $diff2 );

}

Simply pass in your two arrays to that function.

1 Comment

Bear in mind, that this ignores the key's as you stated in your question that you were checking the values
-1

this should work for you:

sort($array1);
reset($array1);
sort($array2);
reset($array2);
$res = array_diff($array1, $array2);

if($res)
    echo "they match";

5 Comments

why are you using usort()?
And without a second parameter? And missing $ from the arrays
Good point. This happenes when you work too much in c )). I hoped to reppresent the idea anyways. Will correct that tho
so... why are you using reset()?
Because it is always a good idea to reset the arrays after manipulating them

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.