Aemz
Aemz

Reputation: 137

delete value in array if duplicates of it found

I have an array like this

$callerid = Array ( [1] => <409> [2] => <3214> [3] => <409> [4] => <5674> ) 

I want to have output like

Array ( [1] => <3214> [2] => <5674> )

That is, I want to remove occurrences of value if found duplicate in array.

how to achieve this?

Upvotes: 1

Views: 325

Answers (2)

Prasanth Bendra
Prasanth Bendra

Reputation: 32740

<?php
$string = Array ( 409,3214,409,5674 ) ;
print_r($string);
foreach($string as $vals){
   $match  = array_keys($string, $vals);
   if(count($match) > 1){
      foreach($match as $ky){
        unset($string[$ky]);
      }
   }

}
print_r($string);
?>

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212412

Doesn't preserve the keys, but returns the correct values (ie, those having an occurrence count of 1)

$callerid = array(1 => 409, 2 => 3214, 3 => 409, 4 => 5674);
$calleridCounts = array_count_values($callerid);
$result = array_keys(
    array_intersect($calleridCounts,array(1))
);
var_dump($result);

Upvotes: 3

Related Questions