gdfhghjdfghgfhf Posted June 3, 2010 Share Posted June 3, 2010 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 !! Quote Link to comment https://forums.phpfreaks.com/topic/203747-keep-only-repeated-items-in-an-array/ Share on other sites More sharing options...
harristweed Posted June 3, 2010 Share Posted June 3, 2010 <?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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/203747-keep-only-repeated-items-in-an-array/#findComment-1067140 Share on other sites More sharing options...
harristweed Posted June 3, 2010 Share Posted June 3, 2010 Wont work, I'll think about MKII Quote Link to comment https://forums.phpfreaks.com/topic/203747-keep-only-repeated-items-in-an-array/#findComment-1067143 Share on other sites More sharing options...
Adam Posted June 3, 2010 Share Posted June 3, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/203747-keep-only-repeated-items-in-an-array/#findComment-1067145 Share on other sites More sharing options...
harristweed Posted June 3, 2010 Share Posted June 3, 2010 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/203747-keep-only-repeated-items-in-an-array/#findComment-1067146 Share on other sites More sharing options...
gwolgamott Posted June 3, 2010 Share Posted June 3, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203747-keep-only-repeated-items-in-an-array/#findComment-1067147 Share on other sites More sharing options...
Adam Posted June 3, 2010 Share Posted June 3, 2010 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.. Quote Link to comment https://forums.phpfreaks.com/topic/203747-keep-only-repeated-items-in-an-array/#findComment-1067149 Share on other sites More sharing options...
gwolgamott Posted June 3, 2010 Share Posted June 3, 2010 if($value == 3) that should be greater then or equal to 3... thanks for point that out and will have to do minor changes... oh that changes things let me get back on that. Quote Link to comment https://forums.phpfreaks.com/topic/203747-keep-only-repeated-items-in-an-array/#findComment-1067151 Share on other sites More sharing options...
gwolgamott Posted June 3, 2010 Share Posted June 3, 2010 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++; } } Quote Link to comment https://forums.phpfreaks.com/topic/203747-keep-only-repeated-items-in-an-array/#findComment-1067153 Share on other sites More sharing options...
ignace Posted June 3, 2010 Share Posted June 3, 2010 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/203747-keep-only-repeated-items-in-an-array/#findComment-1067325 Share on other sites More sharing options...
kenrbnsn Posted June 3, 2010 Share Posted June 3, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/203747-keep-only-repeated-items-in-an-array/#findComment-1067326 Share on other sites More sharing options...
Adam Posted June 4, 2010 Share Posted June 4, 2010 Oh yeah, was overcomplicating things.. Quote Link to comment https://forums.phpfreaks.com/topic/203747-keep-only-repeated-items-in-an-array/#findComment-1067647 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.