...

View Full Version : Compare arrays in MySQL using php?



Hayyel
03-15-2009, 05:17 AM
I have an array I am given on a irregular basis, say 3 - 5 times a week, and it is uploaded to a website via a form and inserted into a MySql table.

I am needing to be able to compare the array I am given today to the table in MySQL and output the newest array and the difference on the same webpage.

Example:

I upload an array of current employees on Mon.
On Wed. I am given the newest array to update the db with but I need to be able to distinguish which employees are in the db and not the newest array.. ie they were fired or quit. On Thurs. I need to do this again.

The unique key is the Employee ID number so I am using REPLACE INTO in my php code.

How can I accomplish this? I was thinking of a flag of some sort... but since it is a irregular update occurrence I cannot think of a way.

Fumigator
03-16-2009, 08:47 AM
How about fetching the data from the table into an array, then comparing that array to the new array to create your report. Then proceed onto the table update using the new array as it's always been done. There's an army of array functions in PHP to make it easy to compare two arrays.

http://us.php.net/manual/en/function.array-diff-assoc.php

Hayyel
03-16-2009, 02:24 PM
I was thinking about this however how do I query the db so array is in the same form? The one being submitted is 2 dimensional.

Submitted array:


Array (
[1] => Array
(
[note] =>
[careerID] => 20
[lastLogin] => 1231125665
[bearerStatus] => 0
[lastLoginNumberHour24] => 22
[dept] => 0
[lastLoginNumberDay] => 4
[statusNumber] => 1
[lastLoginNumberMonth] => 1
[lastLoginNumberYear] => 2009
[memberID] => 28178

)

When i query it I havent found anything on how to setup the numerical key for the outter array.

Fumigator
03-16-2009, 05:17 PM
You'll have to get the two arrays to look the same somehow. Either convert the new array to use your unique primary key for the array index, or something.

If that's too much of a pain, another method that would work is fetch through the existing table and perform an array search for each fetch. You'll need to keep track of array elements that you do find (probably by adding an index that indicates it's been found) so you can go back through the array and determine the ones that are new and should be inserted.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum