Page 1 of 1
Compare 2 arrays ???
Posted: Fri May 20, 2005 4:19 pm
by Sacapuss
Hi !
Could you let me know how i could, with javascript, detect that 2
arrays contain the same elements, please ?
Thanx for your help !
Posted: Wed May 25, 2005 11:10 am
by Chris Corbyn
Code: Select all
var array1 = new Array(
'one',
'two',
'three'
);
var array2 = new Array(
'one',
'two',
'three'
);
var array3 = new Array(
'not one',
'two',
'not four'
);
function arrayDiffer(arr1, arr2) {
//Get length of array (and check for equal length)
l1 = arr1.length;
l2 = arr2.length;
if (l1 != l2) {
return false;
}
for (theKey in arr1) { //Check each element in array1 is same as respective element is array2
if (arr1їtheKey] != arr2їtheKey]) {
return false;
}
}
return true;
}
Thanx for your answer, d11wtq !
Posted: Tue May 31, 2005 12:58 am
by Sacapuss
Thanx for your answer, d11wtq !
Posted: Fri Jul 15, 2005 7:37 pm
by thedamo
maybe I have not understood the question, but could you just use the
array_diff() function to if it returns a 0 length array, you then know that they are the same.
e.g.
Code: Select all
$myArray = array_diff($array1, $array2);
if(count($myArray) < 1)
print("e;arrays are the same"e;);
Posted: Fri Jul 15, 2005 7:49 pm
by Burrito
this was for JS, not php...