I have two arrays:
$a = array('a','b','c');
$b = array('b','c','d','e');
What must I do in order to get array ('a','d','e') via array $a and $b?
$a = array('a','b','c');
$b = array('b','c','d','e');
$output = array_merge(array_diff($a, $b), array_diff($b, $a));
I think that is right - is off top of my head.
Yup http://ideone.com/56V0d0 <- fiddle here - seems to work!
Related
This question already has answers here:
Php match values between 2 arrays
(4 answers)
Closed 6 years ago.
I have two arrays as follows:-
$a = ["2","11","6"];
$b = ["6","7"];
$c = array_diff($a, $b);
$c = ["2","11"];
The result in $c is wrong. I want the result should be as $c = [6]
in other words i want the common elements in both array be returned! but it is giving wrong error. Kindly help me?
Use array_intersect()
$a = ["2","11","6"];
$b = ["6","7"];
$c = array_intersect($a, $b);
Demo: https://eval.in/682653
You can use array_intersect
$c = array_intersect($a, $b);
$a = ["2","11","6"];
$b = ["6","7"];
$c = array_intersect($a,$b);
Use array_intersect (http://php.net/manual/en/function.array-intersect.php)
<?php
$a = ["2","11","6"];
$b = ["6","7"];
$c = array_intersect($a, $b);
print_r($c)
?>
I have an array with possible duplicate values, and I want not only to remove them (i use array_unique for that), but extract them in anothr array.
i.e.
$a = array(1,2,2,3,4,4,5,6,6,6);
$b = array_unique($a); // has 1,2,3,4,5,6
I want a third array ($c) with the duplicates only
that is, with [2,4,6] or [2,4,6,6] (either would do)
whats the easiest way of doing it?
I tried $c = array_diff($a,$b), but gives an empty array, since it is removing all of the occurrences of $b from $a (because, of course, they occur at least once in $b)
I also thought of array_intersect, but it result in an array exactly like $a
Is there a direct function in php to achieve this? How to do it?
I also found such solution on Internet:
$c = array_unique( array_diff_assoc( $a, array_unique( $a ) ) );
But it doesn't seem easy to understand
You can use array_count_values to count the # of occurrences of each element and use array_filter to only keep those that occur more than once.
$a = array(1,2,2,3,4,4,5,6,6,6);
$b = array_count_values($a);
$c = array_filter($b,function($val){ return $val > 1; });
$c = array_keys($c);
print_r($c);
If your input array is sorted you can find dupes by looping through the array and checking if the previous element is equal to the current one
$a = array(1,2,2,3,4,4,5,6,6,6);
$dupes = array();
foreach($a as $i => $v) {
if($i > 0 && $a[--$i] === $v)
$dupes[] = $v;
}
print_r($dupes);
I have 2 arrays.
I want to add the elements of $b to the end of $a. I checked google, nothing.
$a=array(1,2,3);
$b=array(4,5,6);
array_push($a,$b);
print_r($a);
My goal is to make $a=(1,2,3,4,5,6) but the above doesn't work correctly...any ideas?
You can use array_merge for that.
you can use array_merge
$c = array_merge($a, $b);
$c = array(1,2,3,4,5,6);
Use array_merge()
$a = array(1,2,3);
$b = array(4,5,6);
$c = array_merge($a, $b);
print_r($c);
You can't use array operator: + as xzyfer pointed out.
It will do a union and only works when the keys don't overlap.
Here's the situations:
I have 2 arrays, eg:
$a=array('a','b','c','d');
$b=array('1','b','c','e');
I want to produce 2 arrays with result:
$c=array('a','d');//only element appeared on $a
$d=array('1','e');//only element appeared on $b
Do you have a clever solution?
$c = array_diff($a, $b);
$d = array_diff($b, $a);
Sorry, my bad. It turn out a was giving the wrong array in my test.
simple array_diff solved the problem:
$c = array_diff($a, $b);
$d = array_diff($b, $a);
Try using the array_diff() function:
array_diff(array1,array2,array3...)
eg:
<?php
$a1=array(0=>"Cat",1=>"Dog",2=>"Horse");
$a2=array(3=>"Horse",4=>"Dog",5=>"Fish");
print_r(array_diff($a1,$a2));
?>
Output:
Array ( [0] => Cat )
Source: http://www.w3schools.com/PHP/func_array_diff.asp
How else might you compare two arrays ($A and $B )and reduce matching elements out of the first to prep for the next loop over the array $A?
$A = array(1,2,3,4,5,6,7,8);
$B = array(1,2,3,4);
$C = array_intersect($A,$B); //equals (1,2,3,4)
$A = array_diff($A,$B); //equals (5,6,7,8)
Is this the simplest way or is there a way to use another function that I haven't thought of? My goal is to have an array that I can loop over, pulling out groups of related content (I have defined those relationships elsewhere) until the array returns false.
You've got it. Just use array_diff or array_intersect. Doesn't get much easier than that.
Edit:
For example:
$arr_1 = array_diff($arr_1, $arr_2);
$arr_2 = array_diff($arr_2, $arr_1);
Source
Dear easy and clean way
$clean1 = array_diff($array1, $array2);
$clean2 = array_diff($array2, $array1);
$final_output = array_merge($clean1, $clean2);
See also array_unique. If you concatenate the two arrays, it will then yank all duplicates.
Hey, even better solution: array _ uintersect.
This let's you compare the arrays as per array_intersect but then it lets you compare the data with a callback function.
Try to this
$a = array(0=>'a',1=>'x',2=>'c',3=>'y',4=>'w');
$b = array(1=>'a',6=>'b',2=>'y',3=>'z');
$c = array_intersect($a, $b);
$result = array_diff($a, $c);
print_r($result);