Jump to content

keep only repeated items in an array


Recommended Posts

i have an array with a lot of items.... i'm looking for a function to keep only the items repeated at least 3 times

 

example

 

array(

"one item",

"two items",

"two items",

"three items",

"three items",

"three items",

"end"

);

 

 

would become

 

 

array(

"three items",

"three items",

"three items"

);

 

 

 

thanks !!

Link to comment
https://forums.phpfreaks.com/topic/203747-keep-only-repeated-items-in-an-array/
Share on other sites

<?php
$array=array(
"one item",
"two items",
"two items",
"three items",
"three items",
"three items",
"end"
);
$newarray=(array_count_values($array));
foreach ($newarray as $key => $value) {
if($value<3)unset($newarray[$key]);
}
print_r ($newarray);
$flip=array_flip($newarray);
print_r ($flip);
?>

Thought this was going to be easy when I first looked at it, but decided to find a solution for my own curiosity more than anything.

 

It's ugly, but:

 

$array = array(
    'value 1', 'value 1', 'value 1',
    'value 2', 'value 2', 'value 2',
    'value 3', 'value 3',
    'value 4', 'value 5',
);

$counts = array_count_values($array);

foreach ($counts as $count_key => $count)
{
    if ($count < 3)
    {
        foreach ($array as $key => $value)
        {
            if ($value == $count_key)
            {
                unset($array[$key]);
            }
        }
    }
}

 

I wonder if anyone has a better way?

best I can do:

<?php
$array=array(
"one item",
"two items",
"two items",
"three items",
"three items",
"three items",
"end"
);
$newarray=(array_count_values($array));
foreach ($newarray as $key => $value) {
if($value<3)unset($newarray[$key]);
}
print_r ($newarray);

// just reread your post
$try_again=array();
foreach ($newarray as $key => $value) {
    for($i=0;$i<$value;$i++){
        $try_again[]=$key;
    }
}
    print_r ($try_again);
?>

Coule also use array_count_values(), will make your values the keys and the value will be the number of occurrences.

 

<?php
$a=array("Cat","Dog","Horse","Dog");
$b = array();
$b = array_count_values($a);
?>

The value of array $b will be:

Array ( [Cat] => 1 [Dog] => 2 [Horse] => 1 )

 

Then just repopulate your array from that information with a foreach statement

 

$c = array();
$count = 0;
foreach ($b as $key => $value)
{
        if($value == 3) 
       {
         $c[$count] = $key;
         $count++;
         $c[$count] = $key;
         $count++;
         $c[$count] = $key;
         $count++;
       }
}

 

Bit ugly but could be cleaned up some.

Coule also use array_count_values(), will make your values the keys and the value will be the number of occurrences.

 

<?php
$a=array("Cat","Dog","Horse","Dog");
$b = array_count_values($a);
?>

The value of array $b will be:

Array ( [Cat] => 1 [Dog] => 2 [Horse] => 1 )

 

Then just repopulate your array from that information with a foreach statement

 

$c = array();
$count = 0;
foreach ($b as $key => $value)
{
        if($value == 3) 
       {
         $c[$count] = $key;
         $count++;
         $c[$count] = $key;
         $count++;
         $c[$count] = $key;
         $count++;
       }
}

 

Bit ugly but could be cleaned up some.

 

*At least* 3..

Replace that with my original for each statement

foreach ($b as $key => $value)
{
        if($value >= 3) 
       {
         for ($i=value; $i>0; $i--)
           {
              $c[$count] = $key;
              $count++;
           }

         $count++;
       }
}

Thought this was going to be easy when I first looked at it, but decided to find a solution for my own curiosity more than anything.

 

It's ugly, but:

 

$array = array(
    'value 1', 'value 1', 'value 1',
    'value 2', 'value 2', 'value 2',
    'value 3', 'value 3',
    'value 4', 'value 5',
);

$counts = array_count_values($array);

foreach ($counts as $count_key => $count)
{
    if ($count < 3)
    {
        foreach ($array as $key => $value)
        {
            if ($value == $count_key)
            {
                unset($array[$key]);
            }
        }
    }
}

 

I wonder if anyone has a better way?

 

How about? It's that simple or am I missing something?

 

$valueCount = array_count_values($array);
foreach ($array as $key => $value) {
  if ($valueCount[$value] < 3 || $valueCount[$value] > 3) {
    unset($array[$key]);
  }
}

 

Edit: I tested it using the array of MrAdam and got

 

Array

(

    [ 0 ] => value 1

    [ 1 ] => value 1

    [ 2 ] => value 1

    [ 3 ] => value 2

    [ 4 ] => value 2

    [ 5 ] => value 2

)

That's a good solution, except the OP wants to keep all entries repeated 3 or more times, so remove the " || $valueCount[$value] > 3" from the "if" statement.

 

I tested your solution with the array:

Array
(
    [0] => one item
    [1] => two items
    [2] => two items
    [3] => three items
    [4] => three items
    [5] => three items
    [6] => four
    [7] => four
    [8] => four
    [9] => four
    [10] => five
    [11] => end
    [12] => five
    [13] => five
    [14] => five
    [15] => five
)

and got

Array
(
    [3] => three items
    [4] => three items
    [5] => three items
    [6] => four
    [7] => four
    [8] => four
    [9] => four
    [10] => five
    [12] => five
    [13] => five
    [14] => five
    [15] => five
)

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.