Forum Moderators: coopster

Message Too Old, No Replies

finding difference between array values

also needs to be very very efficient

         

httpwebwitch

8:58 pm on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This one needs to find the difference between the values of two arrays. For example:

I have two arrays:
('a'=>1,'b'=>3,'c'=>2,'d'=>6)
('a'=>2,'b'=>0,'c'=>4)

This would subtract the two values with the same key and return an absolute value of the difference, eg:

('a'=>1,'b'=>3,'c'=>2,'d'=>6)

This function could potentially be used in billions of iterations, so it needs to be as efficient as possible.

httpwebwitch

9:03 pm on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



it would be OK if values of 0 are omitted from the array. for example:

('a'=>3,'b'=>7,'c'=>2,'d'=>1)
('a'=>3,'b'=>7,'c'=>4)

could return

('c'=>2,'d'=>1)

either way would be fine; my main concerns are accuracy and extreme efficiency.

coopster

9:09 pm on Jul 17, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I see you already got a response in this thread regarding adding sums of array values [webmasterworld.com], but I'll add that if you don't want zero values you could use array_filter() [php.net] without the callback function. If the callback function is not supplied, array_filter() will remove all the entries of input that are equal to
FALSE
.
return array_filter($c);