Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

My input is

$array2 = array("b" => "green","red");

Array for search

$array1 = array("a" => "green", "green","red", "red", "red");

Output of array1:

Array
(
    [0] => green
)

I need a function finds values in the first array which exist at least three times. These values will be unset from the second array.

share|improve this question
1  
My result is empty Array ( ) not green –  Bora Jun 23 at 9:20
    
@bora same here. eval.in/166020 –  Déjà vu Jun 23 at 9:23
    
It's unclear what you're asking. Please rephrase your question. –  nl-x Jun 23 at 9:26
    
Please specify your input and the expected outcome. –  colburton Jun 23 at 9:29
    
Do you want to remove only triple repetition values, not duplicate? –  Bora Jun 23 at 9:32
show 3 more comments

2 Answers

$array1 = array_unique($array1);

Please do some reserach on your own and provide tangible efforts to solve the issue. In order to remove duplicate entries, there's a pre-written function called array_unique. You can read about it on php.net

share|improve this answer
add comment

The function array_count_values returns an array using the values of the input array as keys and their frequency in it as values (see here).

So, to solve your problem you could use the following function, which is based on array_count_values:

function checkDuplicates($filter,$list,$duplicates = 2)
{
    $frequencies = array_count_values($list);
    $uniques = array_unique($list);
    $result = array();
    foreach($uniques as $value)
    {
        if(!in_array($value,$filter) or $frequencies[$value]<$duplicates)
        {
            $result[] = $value;
        }
    }
    return $result;
}

and call it as follows:

checkDuplicates($array2,$array1,3);

See it working here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.