-1

I have simple foreach loop in an array

<?php  
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {
  echo "$value <br>";
}
?>  

I would like to get the occurence number of the value, something like "$value is X occurence <br>" so the text would be:

red is 0 occurence

green is 1 occurence

....

How can I do that?

servitus
  • 57
  • 6
  • So you want the total number for each element or the position of the element(aka the key)? If you just want the position do `foreach($arr as $position => $value)` – Andrei Feb 05 '18 at 09:00
  • `foreach ($colors as $key=>$value) { echo "$key => $value
    "; }`
    – splash58 Feb 05 '18 at 09:01
  • Possible duplicate of [How to find the foreach index](https://stackoverflow.com/questions/141108/how-to-find-the-foreach-index) – CBroe Feb 05 '18 at 09:01
  • total number, because I have an array and I need to address a specific ID to all of them to make sth like this: `id="carousel-selector-0` `id="carousel-selector-1` `...` – servitus Feb 05 '18 at 09:02
  • `red is 0 occurence` - is that a *total number* ? – splash58 Feb 05 '18 at 09:03
  • this could help you: https://stackoverflow.com/questions/6408095/how-do-i-count-same-values-in-an-array-and-store-it-to-a-variable – molinet Feb 05 '18 at 09:04

5 Answers5

1

USE THIS

<?php  
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $key=>$value) {
  echo $value . " is " . $key . " occurence <br>";
}
?>
Swadesh Ranjan Dash
  • 511
  • 1
  • 4
  • 16
1

When you use foreach, you can have the key as well as the value. Here, since you have not specified the key, the key will be the index value of the array elements. To get the occurence, you need to use foreach($colors as $index => $value)

<?php  
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $index => $value) {
  echo "$value is $index occurence <br>";
} 
Annapurna
  • 429
  • 6
  • 17
1

There is no need to use exotic functions to achieve this. Just replace the foreach loop with a for loop, so you will have a ready-made indexer to use:

<?php

    $colors = array("red", "green", "blue", "yellow");

    for ($i = 0; $i < count($colors); ++$i) {
        echo $colors[$i]." is ".$i." occurrence<br>";
    }

?>

Alternatively, but only because your array doesn't define the keys, you can use the following approach:

<?php

    $colors = array("red", "green", "blue", "yellow");

    foreach ($colors as $index=>$value) {
        echo "$value is $index occurrence<br>";
    }

?>
Tommaso Belluzzo
  • 21,428
  • 7
  • 63
  • 89
0

Use array_count_values function to get desired result.

$colors = array("red", "green", "blue", "yellow");
$counts = array_count_values($colors);

foreach ($counts as $key=>$value) {
  echo $key."=>".$value."<br>"; 
}

See docs http://php.net/manual/en/function.array-count-values.php here

Sagar Gautam
  • 7,648
  • 5
  • 36
  • 65
0

Try this as I suggested in the comments section:

<?php  
$colors = array("red", "green", "blue", "yellow");
$occurences = array_count_values($colors);

foreach ($colors as $value) {
   echo "$value is $occurences[$value] <br>";
}
?>  
molinet
  • 256
  • 3
  • 11