PHP : Getting not matched records from 2 arrays - php

Below are the 2 arrays
$full = array('ABCD19ed81424931667', 'ABCD0c08b1424947569');
$filtered = array('ABCD19ed81424931667');
I want the records which are not matching in 2 arrays. I have tried
array_diff, array_diff_assoc functions it did not work for me.

array_diff returns the difference in 1 direction. To get the difference in both directions you can do 2 array_diff followed by a array_merge:
$output = array_merge(array_diff($full, $filtered), array_diff($filtered, $full));

array_diff — Computes the difference of arrays.
Here array_diff(A,B) and array_diff(B,A) is different.
$full = array('ABCD19ed81424931667', 'ABCD0c08b1424947569');
$filtered = array('ABCD19ed81424931667');
$result = array_merge(array_diff($full, $filtered), array_diff($filtered, $full));
array_diff(A,B) returns all elements from A, which are not elements of B (= A without B). so you need to merge output

Related

How can I compare two nested arrays in PHP?

Recently, I encountered some problem on comparing 2 nested arrays. I have tested to use array_diff_assoc to compare 2 arrays but it return me incorrect. Below are couple scenarios that I am attempt to compare these 2 nested array in php.
$arr1 = ["colorFamily"=>[]];
$arr2 = ["colorFamily"=>["blue","black"]];
$diff = array_diff_assoc($arr1,$arr2);
The $diff is expected to return below;
["colorFamily"=>[]]
Unfortunately, it return below:
[]
Aside from that, there are more complex scenario will be used in the comparison such as
$arr1 = ["colorFamily"=>[],"descriptionMeasurement"=>[["label"=>"orange"],["value"=>"apple"]]];
$arr2 = ["colorFamily"=>["blue","black"],"descriptionMeasurement"=>[["label"=>"orange"]]];
Above scenario, it should return
["colorFamily"=>[],"descriptionMeasurement"=>[["label"=>"orange"]]].
In short, anything occurs in $arr1 and it is not occurs in $arr2, should return in the $diff array.
I hope you guys can help me to resolve this problem, I have been stucking for weeks.
With array_diff you should always put the array with the missing values first, otherwise it doesn't return anything.
Also it can't see past the first array. So something like this will work:
$diff = array_diff_assoc($arr2["colorFamily"], $arr1["colorFamily"])
You could also use the following. array_map is used to index the keys:
$missing = array_map('unserialize', array_diff(array_map('serialize', $arr2), array_map('serialize', $arr1)));
(credits: Use array_diff_assoc() or get difference of multidimensional arrays)

return an array of numbers that show up in all three sub arrays

I want to compare all the sub arrays in the $ids and I want to return only the numbers that show up in all three arrays, so it would return 3 and 4 as an array.
$ids = [
[1,2,3,4],
[2,3,4],
[3,4,5],
];
expecting it to return
array(3,4)
UPDATE: My apologies, I left out the detail that I would not know how many arrays would be in the associative array, it could be 2 sub arrays, other times it could be 5. I appreciate all the answers I have received.
Obviously array_intersect is the solution How to get common values from two different arrays in PHP. If you know the number of sub-arrays and their indexes then it's as simple as:
$result = array_intersect($ids[0], $ids[1], $ids[2]);
However, if you need to do it for an unknown/variable number of sub-arrays and/or unknown indexes, use $ids as an array of arguments with call_user_func_array:
$result = call_user_func_array('array_intersect', $ids);
Or better use Argument unpacking via ... (PHP >= 5.6.0):
$result = array_intersect(...$ids);
Splice out the first subarray and then loop the rest and use array_intersect to filter the result to 3,4.
$res = array_splice($ids,0,1)[0];
foreach($ids as $id){
$res = array_intersect($res, $id);
}
var_dump($res); // 3,4
https://3v4l.org/Z7uZK

array_diff not returning. why?

I've read some tutorials on here but none of them return what I need. I have two arrays.
$a = '5,6,';
$b = '6,6,';
$a_array = explode(',',$a);
$b_array = explode(',',$b);
$result = array_diff($b_array,$a_array);
var_dump($result);
However, when I run array_diff, it returns an empty array.
$result = array_diff($b_array,$a_array);
But I'd like it to return 6. What's the error in my code?
It is because you are comparing the b to the a. The b array contains no elements that are different from elements in a. All the similar elements are not compared to elements in similar positions. They are compared to all elements from the opposite array. If you compared a to b, you would return the 5 value in the result array, because no elements in b have 5. But when you compare b to a, each 6 finds a pair in a, so no differences are found.
$a = '5,6,';
$b = '6,6,';
$a_array = explode(',',$a);
$b_array = explode(',',$b);
$result = array_diff($a_array,$b_array);
var_dump($result);
array_diff() doesn't work like this.
array_diff($b_array,$a_array) would return an array with entries that are present in $b_array but not present in $a_array.
So, in your case, it would check if $a_array contains '6' or not two times( as $b_array would be having two '6') and will found out each time that yes, $a_array contains'6' and hence returns empty array.
For more insights: https://www.w3schools.com/php/func_array_diff.asp

PHP Merge two indexed arrays but NOT normally [duplicate]

This question already has answers here:
Merge two flat indexed arrays of equal size so that values are pushed into the result in an alternating fashion
(2 answers)
Closed last month.
I have two arrays:
$array1 = array("A","One","C","Z");
$array2 = array("B","K","2","5");
Is there some built-in PHP way to get a final array with (I don't know how to say it, maybe 1-1 correspondence addition) alternate keys appended to each other like this
$final_array = array("A","B","One","K","C","2","Z","5");
If I use array_merge, I get:
$final_array = array("A","One","C","Z","B","K","2","5")
But that's exactly what an array_merge does. Is there any workaround other than looping?
Try this:
$array1 = array(1,3,5,7);
$array2 = array(2,4,6,8);
$final_array = array_merge($array1,$array2);
sort($final_array);
print_r($final_array);
Hope this helps. Sorted the array using the sort function.

Opposite of array_intersect?

Is there a built-in function to get all members of array 1 which do not exist in array 2?
I know how to do it programatically, only wondering if there is a built-in function that does the same. So please, no code examples.
That sounds like a job for array_diff.
Returns an array containing all the
entries from array1 that are not
present in any of the other arrays.
array_diff is definitely the obvious choice but it is not technically the opposite of array interesect. Take this example:
$arr1 = array('rabbit','cat','dog');
$arr2 = array('cat','dog','bird');
print_r( array_diff($arr1, $arr2) );
What you want is a result with 'rabbit' and 'bird' in it but what you get is only rabbit because it is looking for what is in the first array but not the second (and not vice versa). to truly get the result you want you must do something like this:
$arr1 = array('rabbit','cat','dog');
$arr2 = array('cat','dog','bird');
$diff1 = array_diff($arr1, $arr2);
$diff2 = array_diff($arr2, $arr1);
print_r( array_merge($diff1, $diff2) );
Note: This method will only work on arrays with numeric keys.
$diff = array_diff($array1, $array2);
array_diff()
Just to clarify as I was looking into this question the answers of #Jon and #Dallas Caley are both correct depending on the domain of your arrays.
If the array against what you are comparing is the full domain of your results then a simple array_diff will suffice as per #Jon answer.
If the array against what you are comparing is NOT the full domain of your results then you should go with the double array_diff as per #Dallas Caley answer.

Categories