简体   繁体   中英

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?

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);

try this:

$array = array(1,4,5,6,4,2,1);
$duplicates = array_unique(array_diff_assoc($array, array_unique($array)));
$a1 = array( 1,4,5,6,4,2,1);
$a = array();
foreach($a1 as $value){
  if(!in_array($value, $a)){
    $a[] = $value;
  }
}
$arrayA = array(1,4,5,6,4,2,1);
$newarray = array_diff_assoc($arrayA, array_unique($arrayA));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM