Reputation: 45
I have an array that stores some values. I'm trying to detect the similar values and add them to new array.
example:
$arrayA = array( 1,4,5,6,4,2,1);
$newarray = (4,1);
Any help?
Upvotes: 0
Views: 119
Reputation: 14523
$arrayA = array(1,4,5,6,4,2,1);
$newarray = array_diff_assoc($arrayA, array_unique($arrayA));
Upvotes: 0
Reputation: 2265
Use the array_intersect() method. For example
$arrayA = array(1,4,5,6,4,2,1);
$arrayB = array(4,1);
$common_values = array_intersect($arrayA, $arrayB);
Upvotes: 1
Reputation: 700
$a1 = array( 1,4,5,6,4,2,1);
$a = array();
foreach($a1 as $value){
if(!in_array($value, $a)){
$a[] = $value;
}
}
Upvotes: 0
Reputation: 35973
try this:
$array = array(1,4,5,6,4,2,1);
$duplicates = array_unique(array_diff_assoc($array, array_unique($array)));
Upvotes: 0