views:

71

answers:

1

Hi, i'm trying to get the difference between two arrays, but with array_diff, array_diff_assoc, array_diff_key i can't get what i want..

Array 1 :
  0 => 424012,
  1 => 423000,
  2 => 425010,
  3 => 431447,
  4 => 421001,
  5 => 421002,

Array 2 :
  0 => 424012,
  1 => 423000,
  2 => 425010,
  3 => 431447,
  4 => 431447,
  5 => 421001,
  6 => 421002,

array_diff =  array () 
// empty

jarray_diff_assoc = array ( 
  4 => 431447,
  5 => 421001,
  6 => 421002,
) 
// OK but too much :)

array_diff_key = array(
6 => 421002
) 
// nope i don't want that :(

i want 431447, cause it's only one time in the first array and twice in the second.

Regards, Tony

+1  A: 

Is that exactly what you want? Only those that occur one time in the first, and two times in the second?

You can basically write your own function for that. Search through the second array, get a list of values that occur two times (or more than once, depending on what it is that you actually want), and then search for those in the first one (this you can do using a built-in PHP function array_intersect).

PawelMysior
Indeed, you'll need to do it manually. Count the elements up in both arrays and then compare.
David Caunt
Yes i know i can do it, but seriously nothing already exists for that ?
Tony Gaillard
what is it that you actually want the function to return?
PawelMysior
the key, the value or what it exists i don't care i just want to get 431447 and others changes values.
Tony Gaillard
I think you misunderstood my question. I get that you want a function that will compare two arrays and return an array of keys, values, whatever. What I want to know, to have a chance to help you, is: How should the function work? Return only those that occur twice in the second array and once in the first? Or return ones that occur more then once in the second and occur in the first? Question is: What is it that you want it to search for exactly.
PawelMysior
It can be only new added values but i don't know how much, the best is : array (431447, 431447) (array of values, i want the new values added in the second array) for two new "431447" but if i can know the number of items added it will be nice
Tony Gaillard
try to explain what is it that you're doing, cause for me its just impossible to understand.
PawelMysior
an array_diff_values which return all the new values
Tony Gaillard