1

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?

3
  • Do you need to preserve the keys or it is not necessary? Commented Jan 28, 2013 at 9:09
  • 1
    array_count_values, remove all elements with a count > 1. Commented Jan 28, 2013 at 9:10
  • Is it important that your input array starts at [1] and your output array too? Commented Jan 28, 2013 at 9:11

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.