compare multi-dimensional arrays and return they keys who's values are different

Reply

Join Date: Nov 2007
Posts: 10
Reputation: BHance is an unknown quantity at this point 
Solved Threads: 0
BHance BHance is offline Offline
Newbie Poster

compare multi-dimensional arrays and return they keys who's values are different

 
0
  #1
Jan 15th, 2009
Hello All,

I have 2 data sources. Data source A needs information to be updated on a regular basis. It receives it's updated information from data source B. There are many thousands of records involved here so I am trying to limit the amount of MySql queries as much as possible. I've managed to get both of these data sources into separate multi-dimensional arrays.

I need to write a function that will compare data source A with data source B and, in a new array, have the keys that need to be changed with only the values that are different.

Here is a visual:

Multi-dimensional array from datasource A
  1. Array
  2. (
  3. ["product_a"] =>
  4. Array (
  5. ["description"]=>"Product A is Large"
  6. ["color"]=>"Red"
  7. ["quantity"]=>5
  8. )
  9. ["product_b"] =>
  10. Array (
  11. ["description"]=>"Product B is Small"
  12. ["color"]=>"Blue"
  13. ["quantity"]=>20
  14. )
  15. ["product_c"] =>
  16. Array (
  17. ["description"]=>"Product C has batteries included"
  18. ["color"]=>"Green"
  19. ["quantity"]=>7
  20. )
  21. )

Multi-dimensional array from datasource B
  1. Array
  2. (
  3. ["product_a"] =>
  4. Array (
  5. ["description"]=>"Product A is Large"
  6. ["color"]=>"Yellow"
  7. ["quantity"]=>8
  8. )
  9. ["product_b"] =>
  10. Array (
  11. ["description"]=>"Product B is Small"
  12. ["color"]=>"Blue"
  13. ["quantity"]=>20
  14. )
  15. ["product_c"] =>
  16. Array (
  17. ["description"]=>"Product C is made of Wax"
  18. ["color"]=>"Green"
  19. ["quantity"]=>7
  20. )
  21. )

Here is what the new array should look like:

  1. Array
  2. (
  3. ["product_a"] =>
  4. Array (
  5. ["color"]=>"Yellow"
  6. ["quantity"]=>8
  7. )
  8. ["product_c"] =>
  9. Array (
  10. ["description"]=>"Product C is made of Wax"
  11. )
  12. )

I know the function probably uses some form of the php built in array_diff function and I am sure there is recursion involved but I can't, for the life of me, figure out how to do this.

I am a beginner programmer so any help would be much appreciated.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 10
Reputation: BHance is an unknown quantity at this point 
Solved Threads: 0
BHance BHance is offline Offline
Newbie Poster

Re: compare multi-dimensional arrays and return they keys who's values are different

 
0
  #2
Jan 16th, 2009
I think I figured it out and it's remarkably simple (almost too simple).

I have two ways of doing it though:

This way seems to perform about 10% faster according to my benchmarks.

  1. foreach ($products as $k1 => $v1) {
  2. if (array_diff($excel_items[$k1], $products[$k1])){
  3. $update_items[$k1] = array_diff($excel_items[$k1], $products[$k1]);
  4. }
  5. }

This way works as well but a tad slower.

  1. foreach ($products as $k1 => $v1) {
  2. $update_items[$k1] = array_diff($excel_items[$k1], $products[$k1]);
  3. if(empty($update_items[$k1])){
  4. unset($update_items[$k1]);
  5. }
  6. }

In both, I am just looping through the keys of the data source A and using array_diff, checking for differences in the other data souce B. If there is a difference I add it to a new array.

The first function works well but I don't like that I have to run the array_diff funtion twice on each iteration (there has to be a cleaner way of doing that).

With the second function I do the array_diff (it returns an empty array if there is nothing different which I thought to be unusual) and then just unset the key if it's empty. I liked this way better but the performance testing is telling me otherwise.

Any ways I can improve this?

Is there anyway to do this better?
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: visser is an unknown quantity at this point 
Solved Threads: 0
visser visser is offline Offline
Newbie Poster
 
0
  #3
21 Days Ago
Originally Posted by BHance View Post
  1. foreach ($products as $k1 => $v1) {
  2. $update_items[$k1] = array_diff($excel_items[$k1], $products[$k1]);
  3. if(empty($update_items[$k1])){
  4. unset($update_items[$k1]);
  5. }
  6. }
This is working a charm, how can I add support for RRP and Cost key's on both Array A and Array B (i.e. if a product RRP or Cost changes the item is added to $update_items).
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the PHP Forum


Views: 1397 | Replies: 2
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC