1

I want to realiaze a php code that help me to detect the content of the variable CEllname and which repeat more than 6 times.

for exemple $cell_old =a,b,c,d,d,d,d,a,d,e,d ( the letter d is repeating for 6 or more )so :

$cell_new=d 

thanks alot

B. Desai
  • 16,414
  • 5
  • 26
  • 47

2 Answers2

2

I cannot tell if $cell_old is an array or a string but here is the code assuming string. If not no need to explode.

$cell_old ='a,b,c,d,d,d,d,a,d,e,d';
//Explode on , if not already an array.
$cell_old_array = explode(',',$cell_old);

$counts = array_count_values($cell_old_array);

The content of $counts is:

Array ( [a] => 2 [b] => 1 [c] => 1 [d] => 6 [e] => 1 )

so now all you would need to do is loop the $counts and store the key of values of 6

foreach($counts as $k => $v){
    if($v >= 6){
        //store $k how you want?
    }
}
nerdlyist
  • 2,842
  • 2
  • 20
  • 32
0
$cell_old = "a,b,c,d,d,d,d,a,d,e,d"; 
$aValues= explode(',' $cell_old); 
$valuescounted = array_count_values($aValues);

foreach($valuescounted as $value => $count) {
    if($count > 6) {
        echo $value .'<br>';
    }
}

array_count_values() will give you an assoc array reveiling the number of times each values appears. Compare that number with 6 to show the desired values.

Ivo P
  • 1,722
  • 1
  • 7
  • 18