| | |
compare multi-dimensional arrays and return they keys who's values are different
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Nov 2007
Posts: 10
Reputation:
Solved Threads: 0
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
Multi-dimensional array from datasource B
Here is what the new array should look like:
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.
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
PHP Syntax (Toggle Plain Text)
Array ( ["product_a"] => Array ( ["description"]=>"Product A is Large" ["color"]=>"Red" ["quantity"]=>5 ) ["product_b"] => Array ( ["description"]=>"Product B is Small" ["color"]=>"Blue" ["quantity"]=>20 ) ["product_c"] => Array ( ["description"]=>"Product C has batteries included" ["color"]=>"Green" ["quantity"]=>7 ) )
Multi-dimensional array from datasource B
PHP Syntax (Toggle Plain Text)
Array ( ["product_a"] => Array ( ["description"]=>"Product A is Large" ["color"]=>"Yellow" ["quantity"]=>8 ) ["product_b"] => Array ( ["description"]=>"Product B is Small" ["color"]=>"Blue" ["quantity"]=>20 ) ["product_c"] => Array ( ["description"]=>"Product C is made of Wax" ["color"]=>"Green" ["quantity"]=>7 ) )
Here is what the new array should look like:
PHP Syntax (Toggle Plain Text)
Array ( ["product_a"] => Array ( ["color"]=>"Yellow" ["quantity"]=>8 ) ["product_c"] => Array ( ["description"]=>"Product C is made of Wax" ) )
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.
•
•
Join Date: Nov 2007
Posts: 10
Reputation:
Solved Threads: 0
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.
This way works as well but a tad slower.
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?
I have two ways of doing it though:
This way seems to perform about 10% faster according to my benchmarks.
PHP Syntax (Toggle Plain Text)
foreach ($products as $k1 => $v1) { if (array_diff($excel_items[$k1], $products[$k1])){ $update_items[$k1] = array_diff($excel_items[$k1], $products[$k1]); } }
This way works as well but a tad slower.
PHP Syntax (Toggle Plain Text)
foreach ($products as $k1 => $v1) { $update_items[$k1] = array_diff($excel_items[$k1], $products[$k1]); if(empty($update_items[$k1])){ unset($update_items[$k1]); } }
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?
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
Solved Threads: 0
0
#3 21 Days Ago
•
•
•
•
PHP Syntax (Toggle Plain Text)
foreach ($products as $k1 => $v1) { $update_items[$k1] = array_diff($excel_items[$k1], $products[$k1]); if(empty($update_items[$k1])){ unset($update_items[$k1]); } }
![]() |
Other Threads in the PHP Forum
- Previous Thread: cURL doesn't return the page?
- Next Thread: Search for FullName with spaces in a DB
Views: 1397 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array basic beginner binary bounce broken cakephp checkbox class cms code cron curl database date directory display domain download dynamic echo email error file files folder form forms function functions google href htaccess html image include indentedsubcategory insert integration ip java javascript joomla limit link load login loop mail menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote return script search select server sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video web xml youtube





