Count times an item is repeated in a php fix

4

Is there a function in php that returns me the number of times an element is repeated within a array ? If this is not the case, I will have to go through each element and, through a counter, see how many times it repeats.

Something like that, like the function count() that has Python . The output of that program would be 2, since the one is present twice in array .

    
asked by Patricio Nicolas 03.11.2016 в 04:29
source

4 answers

3

With the function array_count_values

$valores = array_count_values($array);

The answer is given to you as many as the number and how many times it is repeated

Array
(
       [1] => 2
       [2] => 2
       [3] => 1
)
    
answered by 03.11.2016 / 04:38
source
2

I see two options, which would not be inconvenient when looking for values with commas or quotes.

The first option would be the one that mention @sioesi and @alvaro-montoro , to be native is probably very fast . In this case, you have to be careful because if the value does not exist, you can not find it:

<?php
function contar_valores($a,$buscado)
 {
  if(!is_array($a)) return NULL;
  $v=array_count_values($a);
  return array_key_exists($buscado,$a)?$v[$buscado]:0;
 }

But yes, if you are interested in comparing a single value, you are creating an array in memory with one element for each unique value. For example, array(1,2,3,4,5,6,7,8,9,10,1) , which is 11 long, generates another 10 long elements:

array(
       [1]=>2,
       [2]=>1,
       [3]=>1,
       [4]=>1,
       [5]=>1,
       [6]=>1,
       [7]=>1,
       [8]=>1,
       [9]=>1,
       [10]=>1,
)

With those amounts there should be no problem, but if you treat long arrays it can consume a lot of memory, because you create one of similar size.

On the other hand, if you try to count a single value, it is easier to go through the array and compare each value, using foreach . You can do it in a single line (2 instructions) if you are sure that $a is an array: $i=0;foreach($a as $v) if($buscado===$v) $i++;

If you want it as a function, you can do the following:

<?php
function contar_valores($a,$buscado)
 {
  if(!is_array($a)) return NULL;
  $i=0;
  foreach($a as $v)
   if($buscado===$v)
    $i++;
  return $i;
 }

Thus, it should be more efficient in memory management, since it only creates a numeric variable.

    
answered by 03.11.2016 в 07:23
1

There is no function that returns exactly the number of times a particular value appears, but you could use the function array_count_values to count how many times all the data in the array appears.

From there you could create your own function:

function cuenta_veces_valor($array, $valor) {
    $contadores = array_count_values($array);
    return $contadores[$valor];
}

Although I imagine that, unless you need to count more than one element, it can be a bit excessive.

    
answered by 03.11.2016 в 04:39
0

Try this:

$arr = array('amigo','Hola','amigo','amigo','samigo','amigos','que','tal','tal','tal','amigo');
$str = ", " . implode(", ",$arr) . ",";
$count = substr_count($str, ' amigo,');
echo $str."<br>";
echo $count;

Basically we pass the array to string and then substr_count () makes us work, you will see that the output is "3" because the word "friend" appears three times in your array.

EYE that the element% co_of equal% will add 'amigo,' to +1 , since it has a comma at the end, but you do not want to post it because it is a different element to the element 'friend', which does not have a comma.

Note that this could be inefficient in memory management if you process large amounts of data (you are copying all data in a string).

    
answered by 03.11.2016 в 04:49