Help on iterating 2 arrays and also comparing the values?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

tomex1
Forum Commoner
Posts: 26
Joined: Sun Aug 16, 2009 11:34 am

Help on iterating 2 arrays and also comparing the values?

Post by tomex1 »

Hi all,
I can get my head round how to go about this - Basically, I have got two query resultset from two tables which I am trying to compare their results and also update one of the tables from which these results came from.

For example, if result A is 1,2,3,4 and result B is 1,2,5,4 the output of result be at the end of the comparison should be 1,2,3,4.

Cheers
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: Help on iterating 2 arrays and also comparing the values?

Post by juma929 »

Hello,

So you want a final array as a result of the comparison between the first two arrays?

Not fully sure what your trying to achieve sorry, im leaning towards a couple of array functions that exist but im not sure if that is exactly what you are going to need.
tomex1
Forum Commoner
Posts: 26
Joined: Sun Aug 16, 2009 11:34 am

Re: Help on iterating 2 arrays and also comparing the values?

Post by tomex1 »

Hi juma929,
yes I want an array as a result of the comparison so that I can update that row in the database.

cheers
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Help on iterating 2 arrays and also comparing the values?

Post by Mark Baker »

have you looked at the array_diff() and array_intersect() functions?
tomex1
Forum Commoner
Posts: 26
Joined: Sun Aug 16, 2009 11:34 am

Re: Help on iterating 2 arrays and also comparing the values?

Post by tomex1 »

To be honest, I have but I don't really get how to apply it here.
tomex1
Forum Commoner
Posts: 26
Joined: Sun Aug 16, 2009 11:34 am

Re: Help on iterating 2 arrays and also comparing the values?

Post by tomex1 »

I know array_diff() only returns the difference in the two array which isn't really applicable here cos not only do I want to find out which row is not equal between the two resultset I also need to update that row. Or maybe I am just confusing my self?
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: Help on iterating 2 arrays and also comparing the values?

Post by juma929 »

Hello,

Take a look at array_diff or array_intersect and see if thats what your looking for.

http://us2.php.net/manual/en/function.a ... ersect.php
http://us2.php.net/manual/en/function.array-diff.php

Hope that helps, if not, get back in touch :)
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: Help on iterating 2 arrays and also comparing the values?

Post by juma929 »

DOH

Just noticed other people are talking to you too and they have already mentioned those functions ha-ha.

Sorry, I gotta get in the habit of catching up instead of just replying!
tomex1
Forum Commoner
Posts: 26
Joined: Sun Aug 16, 2009 11:34 am

Re: Help on iterating 2 arrays and also comparing the values?

Post by tomex1 »

:) That's okay. array_diff works for another scenario which I am also using. Baiscally, I use it to update table B when table A has a new row. So I select everything in Table A and also in table B and then use array_diff to find out the newly inserted rows in A which I then insert into B and that works perfectly/
tomex1
Forum Commoner
Posts: 26
Joined: Sun Aug 16, 2009 11:34 am

Re: Help on iterating 2 arrays and also comparing the values?

Post by tomex1 »

The problem is, in this other situation - take for example if the age of tom changes from 5 to 6 in table A I need to update tom's age in table B and these two tables are on different databases. Or maybe my question should be, how do you think I can go about it?

Thanks a lot.
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: Help on iterating 2 arrays and also comparing the values?

Post by juma929 »

Hello,

What database are you using? Is this MySQL?

Thanks
tomex1
Forum Commoner
Posts: 26
Joined: Sun Aug 16, 2009 11:34 am

Re: Help on iterating 2 arrays and also comparing the values?

Post by tomex1 »

No, I am using Apache Derby.
User avatar
mrvijayakumar
Forum Commoner
Posts: 58
Joined: Tue Aug 18, 2009 12:39 am
Location: Chennai city, India
Contact:

Re: Help on iterating 2 arrays and also comparing the values?

Post by mrvijayakumar »

Hi all,
I written coding to solve this issue, i hope this link will help or this will tune some brains to come out with better output. Please tell me your feedback's about this link ,

http://www.vijayakumar.org/index.php/20 ... ay-on-php/
Last edited by mrvijayakumar on Fri Aug 28, 2009 9:40 pm, edited 2 times in total.
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: Help on iterating 2 arrays and also comparing the values?

Post by juma929 »

Hello,

I honestly think if you take a look at JOINs, Sub Queries and Unions, you can solve this problem with no processing on PHP's side. BUT, I dont know Derby, I dont know what it's capable of.

Thanks
tomex1
Forum Commoner
Posts: 26
Joined: Sun Aug 16, 2009 11:34 am

Re: Help on iterating 2 arrays and also comparing the values?

Post by tomex1 »

Wow mrvijayakumar that's brilliant!!!
What happens if table two had more than one row with the same value in it? For example if the age of Tom in Table one has been changed from 5 to 6 and it is still 5 in table two, will it update just that single Tom's age in table two or will it update every record that has 5 in it to 6?

Cheers
Post Reply