Jump to content

Array function


abch624

Recommended Posts

I would like to know how many time this "$key" has been repeated in the array.

Please help

 

as Barand helpfully pointed out:

A key value can only occur once in an array

 

Maybe you want repeated values.

$array = array("apple" => "green",
                    "apple" => "red",
                    "strawberry" => "red"
             );

Are you trying to see how many times apple is in the array, or red?

apple = key

red = value

 

 

Link to comment
https://forums.phpfreaks.com/topic/113487-array-function/#findComment-583230
Share on other sites

try

<?php
$tags = array ('A', 'B', 'A', 'C', 'D', 'B', 'A', 'C', 'A', 'C', 'D', 'E');

$tagCounts = array_count_values($tags);

foreach ($tags as $k => $v)
{
echo "$k : $v (occurs {$tagCounts[$v]} times)<br/>";
}
?>

-->
0 : A (occurs 4 times)
1 : B (occurs 2 times)
2 : A (occurs 4 times)
3 : C (occurs 3 times)
4 : D (occurs 2 times)
5 : B (occurs 2 times)
6 : A (occurs 4 times)
7 : C (occurs 3 times)
8 : A (occurs 4 times)
9 : C (occurs 3 times)
10 : D (occurs 2 times)
11 : E (occurs 1 times)

Link to comment
https://forums.phpfreaks.com/topic/113487-array-function/#findComment-583331
Share on other sites

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.