note 110572 added to function.array-diff
| From: | JeppeUtzon | Date: | Thu, 08 Nov 2012 14:28:11 +0000 |
| Subject: | note 110572 added to function.array-diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-191810@lists.php.net to get a copy of this message | ||
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
?>
The function returns true only if the two arrays contain the same number of values and each value in
one array has an exact duplicate in the other array. Everything else will return false.
----
Server IP: 195.184.96.15
Probable Submitter: 85.233.233.180
----
Manual Page -- http://www.php.net/manual/en/function.array-diff.php
Edit -- https://master.php.net/note/edit/110572
Del: integrated -- https://master.php.net/note/delete/110572/integrated
Del: useless -- https://master.php.net/note/delete/110572/useless
Del: bad code -- https://master.php.net/note/delete/110572/bad+code
Del: spam -- https://master.php.net/note/delete/110572/spam
Del: non-english -- https://master.php.net/note/delete/110572/non-english
Del: in docs -- https://master.php.net/note/delete/110572/in+docs
Del: other reasons-- https://master.php.net/note/delete/110572
Reject -- https://master.php.net/note/reject/110572
Search -- https://master.php.net/manage/user-notes.php