Reputation:
I have two arrays where the first array keys are similar to second array values. I'd like to get a new array with values which are not in the first one. I tried to use array_intersect
, but the result wasn't what I expected.
Here is a sample of the two arrays:
$array1 = array(
'A' => 10,
'B' => 20,
'C' => 30,
'D' => 40,
);
$array2 = array(
'0' => 'A',
'1' => 'B',
);
And I'm looking for a new array like this:
$array3 = array(
'0' => 'C',
'1' => 'D',
);
Upvotes: 1
Views: 78
Reputation: 137
My advice is to use array_diff
, but the problem is that it is not setting keys
$array3 = array_diff(array_keys($array1, $array2))
Upvotes: 0
Reputation: 701
You can use this code:
$array1 = array('A' => 10,'B' => 20,'C' => 30,'D' => 40);
$array2 = array('0' => 'A','1' => 'B');
$array3 = array_keys(array_diff_key($array1, array_flip($array2)));
Upvotes: 1