Page 1 of 2
Help on iterating 2 arrays and also comparing the values?
Posted: Wed Aug 19, 2009 8:35 am
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
Re: Help on iterating 2 arrays and also comparing the values?
Posted: Wed Aug 19, 2009 8:45 am
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.
Re: Help on iterating 2 arrays and also comparing the values?
Posted: Wed Aug 19, 2009 9:08 am
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
Re: Help on iterating 2 arrays and also comparing the values?
Posted: Wed Aug 19, 2009 9:13 am
by Mark Baker
have you looked at the array_diff() and array_intersect() functions?
Re: Help on iterating 2 arrays and also comparing the values?
Posted: Wed Aug 19, 2009 9:14 am
by tomex1
To be honest, I have but I don't really get how to apply it here.
Re: Help on iterating 2 arrays and also comparing the values?
Posted: Wed Aug 19, 2009 9:16 am
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?
Re: Help on iterating 2 arrays and also comparing the values?
Posted: Wed Aug 19, 2009 9:17 am
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

Re: Help on iterating 2 arrays and also comparing the values?
Posted: Wed Aug 19, 2009 9:18 am
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!
Re: Help on iterating 2 arrays and also comparing the values?
Posted: Wed Aug 19, 2009 9:21 am
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/
Re: Help on iterating 2 arrays and also comparing the values?
Posted: Wed Aug 19, 2009 9:24 am
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.
Re: Help on iterating 2 arrays and also comparing the values?
Posted: Wed Aug 19, 2009 9:32 am
by juma929
Hello,
What database are you using? Is this MySQL?
Thanks
Re: Help on iterating 2 arrays and also comparing the values?
Posted: Wed Aug 19, 2009 9:34 am
by tomex1
No, I am using Apache Derby.
Re: Help on iterating 2 arrays and also comparing the values?
Posted: Wed Aug 19, 2009 9:43 am
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/
Re: Help on iterating 2 arrays and also comparing the values?
Posted: Wed Aug 19, 2009 9:54 am
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
Re: Help on iterating 2 arrays and also comparing the values?
Posted: Wed Aug 19, 2009 10:01 am
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