user2352548
user2352548

Reputation: 45

Similar values in php array

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

Answers (4)

MD SHAHIDUL ISLAM
MD SHAHIDUL ISLAM

Reputation: 14523

$arrayA = array(1,4,5,6,4,2,1);
$newarray = array_diff_assoc($arrayA, array_unique($arrayA));

Upvotes: 0

ritesh
ritesh

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

Manish Goyal
Manish Goyal

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

Alessandro Minoccheri
Alessandro Minoccheri

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

Related Questions