imperium2335
imperium2335

Reputation: 24132

Strip all elements from array where they occur less than twice

I have an array like this:

[0] = 2
[1] = 8
[2] = 7
[3] = 7

And I want to end up with an array that looks like:

[0] = 7
[1] = 7

Basically, remove all elements where they occur less than twice.

Is their a PHP function that can do this?

Upvotes: 3

Views: 328

Answers (8)

user2339411
user2339411

Reputation:

$arrMultipleValues = array('2','3','5','7','7','8','2','9','11','4','2','5','6','1');

function array_not_unique($input)    
{
    $duplicatesValues = array();

    foreach ($input as $k => $v)
    {
        if($v>1)
        {
        $arrayIndex=count($duplicatesValues);
        array_push($duplicatesValues,array_fill($arrayIndex, $v, $k));
        }    
    }

    return $duplicatesValues;
}

$countMultipleValue = array_count_values($arrMultipleValues);
print_r(array_not_unique($countMultipleValue));

Upvotes: 1

AlexP
AlexP

Reputation: 9857

This was actually harder to do than i thought...anyway...

 $input  = array(2, 8, 7, 7, 9, 9, 10, 10);
 $output = array();

 foreach(array_count_values($input) as $key => $value) {
   if ($value > 1) $output = array_merge($output, array_fill(0, $value, $key));
 }
 var_dump($output);  

Upvotes: 1

fullybaked
fullybaked

Reputation: 4127

Something like this would work, although you could probably improve it with array_reduce and an anonymous function

<?php

$originalArray = array(2, 8, 7, 7);

foreach (array_count_values($originalArray) as $k => $v) {
    if ($v < 2) {
        $originalKey = array_search($k, $originalArray);
        unset($originalArray[$originalKey]);
    }
}

var_dump(array_values($originalArray));

Upvotes: 2

S&#233;bastien Renauld
S&#233;bastien Renauld

Reputation: 19672

You could use a combination of array_count_values (which will give you an associative array with the value as the key, and the times it occurs as the value), followed by a simple loop, as follows:

$frequency = array_count_values($yourArray);
foreach ($yourArray as $k => $v) {
   if (!empty($frequency[$v]) && $frequency[$v] < 2) {
      unset($yourArray[$k]);
   }
}

I did not test it, but I reckon it works out of the box. Please note that you will loop over your results twice and not N^2 times, unlike an array_search method. This can be further improved, and this is left as an exercise for the reader.

Upvotes: 1

rcbevans
rcbevans

Reputation: 8901

This does the job, maybe not the most efficient way. I'm new to PHP myself :)

   <?php
$element = array();
$element[0] = 2;
$element[1] = 8;
$element[2] = 7;
$element[3] = 7;

$count = array_count_values($element);
var_dump($element);
var_dump($count);

$it = new RecursiveIteratorIterator( new RecursiveArrayIterator($count));

$result = array();
foreach ($it as $key=>$val){
    if ($val >= 2){
        for($i = 1; $i <= $val; $i++){
            array_push($result,$key);
        }
    }
}
var_dump($result);

?>

EDIT: var_dump is just so you can see what's going on at each stage

Upvotes: 0

Yadav Chetan
Yadav Chetan

Reputation: 1894

try this,

$ar1=array(2,3,4,7,7);
    $ar2=array();
    foreach (array_count_values($ar1) as $k => $v) {
        if ($v > 1) {
            for($i=0;$i<$v;$i++)
            {
            $ar2[] = $k;
            }
        }
    }
    print_r($ar2);

output

Array ( [0] => 7 [1] => 7 )

Upvotes: 5

Mark Baker
Mark Baker

Reputation: 212452

$testData = array(2,8,7,7,5,6,6,6,9,1);

$newArray = array();
array_walk(
    array_filter(
        array_count_values($testData),
        function ($value) {
            return ($value > 1);
        }
    ),
    function($counter, $key) use (&$newArray) {
        $newArray = array_merge($newArray,array_fill(0,$counter,$key));
    }
);

var_dump($newArray);

Though it'll give a strict standards warning. To avoid that, you'd need an interim stage:

$testData = array(2,8,7,7,5,6,6,6,9,1);

$newArray = array();
$interim = array_filter(
    array_count_values($testData),
    function ($value) {
        return ($value > 1);
    }
);
array_walk(
    $interim,
    function($counter, $key) use (&$newArray) {
        $newArray = array_merge($newArray,array_fill(0,$counter,$key));
    }
);

var_dump($newArray);

Upvotes: 1

hakre
hakre

Reputation: 198117

Is their [sic!] a PHP function that can do this?

No, PHP has no built-in function (yet) that can do this out of the box.

That means, if you are looking for a function that does this, it needs to be in PHP userland. I would like to quote a comment under your question which already suggest you how you can do that if you are looking for that instead:

array_count_values() followed by a filter with the count >1 followed by an array_fill() might work

By Mark Baker 5 mins ago

If this sounds a bit cryptic to you, those functions he names are actually built-in function in PHP, so I assume this comes most close to the no, but answer:

Upvotes: 0

Related Questions