Determine if two flat arrays contain the same data even if their values are...
Is there anyway to compare arrays in PHP using an in-built function, short of doing some sort of loop?$a1 = array(1,2,3);$a2 = array(1,2,3);if (array_are_same($a1, $a2)) { // code here}Btw, the array...
View ArticleAnswer by Alex Martelli for Determine if two flat arrays contain the same...
Just check $a1 == $a2 -- what's wrong with that?
View ArticleAnswer by Alan Haggai Alavi for Determine if two flat arrays contain the same...
if ( $a == $b ) { echo 'We are the same!';}
View ArticleAnswer by aukion for Determine if two flat arrays contain the same data even...
if(count($a1) == count($a2)){ $result= array_intersect($a1, $a2); if(count($a1) == count($result)) echo 'the same'; else echo 'a1 different than a2';} else echo 'a1 different than a2';
View ArticleAnswer by MWt for Determine if two flat arrays contain the same data even if...
array_intersect() returns an array containing all the common values.
View ArticleAnswer by pscheit for Determine if two flat arrays contain the same data even...
@Cleanshooter i'm sorry but this does not do, what it is expected todo: (so does array_diff mentioned in this context)$a1 = array('one','two');$a2 =...
View ArticleAnswer by Chintan for Determine if two flat arrays contain the same data even...
try this :$array1 = array("street" => array("Althan"), "city" => "surat", "state" => "guj", "county" => "india"); $array2 = array("street" => array("Althan"), "city" => "surat",...
View ArticleAnswer by chichilatte for Determine if two flat arrays contain the same data...
A speedy method for comparing array values which can be in any order...function arrays_are_same($array1, $array2) { sort($array1); sort($array2); return $array1==$array2;}So for... $array1 =...
View ArticleAnswer by pnomolos for Determine if two flat arrays contain the same data...
As far as I can tell, there isn't a single built-in function that'll do it for you, however, something like:if (count($a) == count($b) && (!count(array_diff($a, $b))) { // The arrays are the...
View ArticleAnswer by Tyron for Determine if two flat arrays contain the same data even...
Conclusion from the commentary here:Comparison of array values being equal (after type juggling) and in the same order only:array_values($a1) == array_values($a2)Comparison of array values being equal...
View ArticleAnswer by rcourtna for Determine if two flat arrays contain the same data...
verify that the intersections count is the same as both source arrays$intersections = array_intersect($a1, $a2);$equality = (count($a1) == count($a2)) && (count($a2) == count($intersections)) ?...
View ArticleAnswer by 1Rhino for Determine if two flat arrays contain the same data even...
Compare values of two arrays:$a = array(1,2,3);$b = array(1,3,2);if (array_diff($a, $b) || array_diff($b, $a)) { echo 'Not equal';}else{ echo 'Equal';}
View ArticleAnswer by hakre for Determine if two flat arrays contain the same data even...
Comparing two arrays to have equal values (duplicated or not, type-juggling taking into account) can be done by using array_diff() into both directions:!array_diff($a, $b) && !array_diff($b,...
View ArticleAnswer by CJ Ramki for Determine if two flat arrays contain the same data...
get the array from user or input the array values.Use sort function to sort both arrays. check using ternary operator. If the both arrays are equal it will print arrays are equal else it will print...
View ArticleAnswer by Murz for Determine if two flat arrays contain the same data even if...
Also you can try this way:if(serialize($a1) == serialize($a2))
View ArticleAnswer by Case for Determine if two flat arrays contain the same data even if...
If you do not care about keys and that just the values then this is the best method to compare values and make sure the duplicates are counted.$mag = '{"1":"1","2":"2","3":"3","4":"3"}';$mag_evo =...
View ArticleAnswer by revoke for Determine if two flat arrays contain the same data even...
If you want to search some array inside a big array of arrays (>10k) then much more quickly is comparing serialized arrays (saved in cache). Example from work with URL:/**@return array*/function...
View ArticleAnswer by Ibnul Husainan for Determine if two flat arrays contain the same...
this is a simple way to get the difference:$array1 = [40,10,30,20,50];$array2 = [10,30];$result = array_filter( $array1, function($var) use($array2){ if(in_array($var, $array2)){ return $var; } }...
View ArticleAnswer by AlexisAmasis for Determine if two flat arrays contain the same data...
I just wanted to contribute that:echo var_export([1 => 2, 0 => 1, 3] == [1, 2, 3], 1);Echoes true. Therefore the == operator does not check that the elements in the array are in the same order,...
View ArticlePHP compare array
Is there anyway to compare arrays in php using an inbuilt function, short of doing some sort of loop?$a1 = array(1,2,3);$a2 = array(1,2,3);if (array_are_same($a1, $a2)) { // code here}Btw, the array...
View Article