PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Dec 19, 2014 6:55 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Compare 2 arrays ???
PostPosted: Fri May 20, 2005 4:19 pm 
Offline
Forum Commoner
User avatar

Joined: Mon May 16, 2005 9:46 pm
Posts: 40
Location: Earth...
Hi !

Could you let me know how i could, with javascript, detect that 2
arrays contain the same elements, please ?

Thanx for your help !


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 11:10 am 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia
Syntax: [ Download ] [ Hide ]
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;
}


Top
 Profile  
 
PostPosted: Tue May 31, 2005 12:58 am 
Offline
Forum Commoner
User avatar

Joined: Mon May 16, 2005 9:46 pm
Posts: 40
Location: Earth...
Thanx for your answer, d11wtq !


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 7:37 pm 
Offline
Forum Newbie

Joined: Fri Jul 15, 2005 7:23 am
Posts: 9
Location: Sydney Australia
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.

Syntax: [ Download ] [ Hide ]
$myArray = array_diff($array1, $array2);
if(count($myArray) < 1)
print(&quote;arrays are the same&quote;);


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 7:49 pm 
Offline
Spockulator
User avatar

Joined: Wed Feb 04, 2004 9:15 pm
Posts: 4712
Location: Eden, Utah
this was for JS, not php...


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group