Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] Comparing two arrays


[SOLVED] Comparing two arrays

Reply
  #1 (permalink)  
Old 04-08-08, 11:10 AM
userd userd is offline
Newbie Coder
 
Join Date: Apr 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Comparing two arrays

Hello,

I need you help folks. Struck in between.

I got two arrays.
PHP Code:

$a1 = array(-0.005584046,-0.029838394,-0.02776031);
$a2 = array(-0.001116809,-0.006861126,-0.011040963); 
I want to compare both the arrays and store the result in third array.

Any help will be appreciated.

Thanks
UserD

Last edited by Nico; 04-08-08 at 11:13 AM. Reason: [php] wrappers.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 04-08-08, 11:19 AM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
When you say compare... you want to add them up? You want to see if they are identical? You want to add or remove similar or dissimilar values?

If you could, please give the desired result you'd expect to get from the given arrays.
__________________
The toxic ZCE
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 04-08-08, 11:29 AM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
As Keith has said, without more information as to what you are wanting to do with the values, it's hard to guide you here. I suggest you take a look at the manual for a list of array functions you can use (Go down to the Table of Contents area)

Jay
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 04-08-08, 11:46 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by Keith View Post
When you say compare... you want to add them up? You want to see if they are identical? You want to add or remove similar or dissimilar values?
The answer is "yes".
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 04-08-08, 12:09 PM
userd userd is offline
Newbie Coder
 
Join Date: Apr 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry friends, just couldn't complete my question. I need to compare the first value of array1 with the first value of array2; if a1<a2, I need to store "B" to array3, else "S". The same will be from 1st element to last element of array.

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 04-08-08, 12:27 PM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Something like this?
PHP Code:

$a1 = array(-0.005584046,-0.029838394,-0.02776031);
$a2 = array(-0.001116809,-0.006861126,-0.011040963);
$a3=array();
foreach(
$a1 as $k=>$v)
{
$a3[]=($v<$a2[$k])?'B':'S';
}

print_r($a3); 
Jay
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 04-08-08, 12:34 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Quote:
Originally Posted by End User View Post
The answer is "yes".
Ah, of course!
__________________
The toxic ZCE
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 04-08-08, 12:46 PM
userd userd is offline
Newbie Coder
 
Join Date: Apr 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks a lot Jay. It works perfectly.

Bye!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 04-08-08, 01:50 PM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Keith View Post
Ah, of course!
LOL

Quote:
Originally Posted by userd View Post
Thanks a lot Jay. It works perfectly.

Bye!
No worries. See ya!
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 04-08-08, 03:11 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Quote:
Originally Posted by userd View Post
Sorry friends, just couldn't complete my question. I need to compare the first value of array1 with the first value of array2; if a1<a2, I need to store "B" to array3, else "S". The same will be from 1st element to last element of array.
Now how were we unable to figure out that on our own?
__________________
The toxic ZCE
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
work on arrays within arrays - i'm stuck... nassau PHP 1 04-29-06 03:37 PM
Passing Session Arrays DAL PHP 4 01-07-06 06:56 PM
Arrays ArChAnGeL_J C/C++ 3 11-07-05 10:01 PM
Arrays with sub arrays perleo PHP 1 09-30-05 08:15 PM
Can record arrays on database? mhs12grade1992 PHP 5 02-17-05 12:20 PM


All times are GMT -5. The time now is 01:12 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.