Jump to content

Recommended Posts

Hi All,

 

I would like to find out the count of a particular number in an array and every time that number is found the counter should increase.

For example: There's an array containing numbers and I would like to find out how many of them are less than 45. If a number less than 45 is found the counter should increase.

 

I know I should use loop with mathematicl function < and variable counter but I don't know where to start. I searched in PHP manual and couldn't find one. Is there a specific function for this? I am still new to php and your help is much appreciated.

 

thanks in advance,

nirvana

<?php
  $array = array(1, "hello", 1, "world", "hello");
  $arr_count = array_count_values($array);
$counter =0;
foreach($arr_count as $key => $value){
     if($value < 45){
            $counter = $counter + 1;  
    }
}
echo $counter;
?>

 

array_count_values

(PHP 4, PHP 5)

 

array_count_values -- Counts all the values of an array

Description

array array_count_values ( array input )

 

 

array_count_values() returns an array using the values of the input array as keys and their frequency in input as values.

 

Example 1. array_count_values() example

 

<?php

$array = array(1, "hello", 1, "world", "hello");

print_r(array_count_values($array));

?> 

 

The above example will output:

 

Array

(

    [1] => 2

    [hello] => 2

    [world] => 1

)

 

 

 

 

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.