Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.