Quantcast
Channel: Determine if two flat arrays contain the same data even if their values are not in the same order - Stack Overflow
Browsing index pages (38 articles)

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 Article


Answer by Alex Martelli for Determine if two flat arrays contain the same...

Just check $a1 == $a2 -- what's wrong with that?

View Article


Answer by Alan Haggai Alavi for Determine if two flat arrays contain the same...

if ( $a == $b ) { echo 'We are the same!';}

View Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

PHP 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
Browsing index pages (38 articles)


Latest Images