To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Coding

Coding Help with PHP coding

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 11-03-2004, 04:33 PM   #1
viveleroi0
Senior Member
 
viveleroi0's Avatar
 
Join Date: May 2002
Posts: 276
count occurrences of number in an array?

How would I count the number of times a specific number occurrs in an array?

I know how to see if it's in the array, I just need to know how many times.
__________________
been here twice as long as I thought and know half of what I think
viveleroi0 is offline   Reply With Quote
Old 11-03-2004, 05:18 PM   #2
bubblenut
Copyleft Commie
 
bubblenut's Avatar
 
Join Date: Oct 2003
Location: London
Posts: 2,372
array_count_values()
or to more specifically answer your request.
PHP Code:
function array_count_this_value($array, $value) {
  
$values=array_count_values($array);
  return
$values[$value];
}
__________________
If at first it doesn't work, slap it with a dirty hack.

Moral of the week: Never let a moral of the week go on for more than a week, it's even sillier than feeding the admins.

My Blog
bubblenut is offline   Reply With Quote
Old 11-03-2004, 05:30 PM   #3
drawmack
Computers can do that?
 
drawmack's Avatar
 
Join Date: Apr 2003
Location: Pocono Mtns PA
Posts: 3,268
You could also use a little recursion and some self counting like this:
PHP Code:
<?php
function howMany($needle,$haystack) {
    
$exists = array_search($needle,$haystack);
    
//if the desired element exists in the array
    
if ($exists !== FALSE)
        
//return 1 and call the function again with the array after the
        //first occurance of needle
        
return 1 + howMany($needle,array_slice($haystack,($exists+1)));

    
//if the element does not exist return 0;
    
return 0;
}
//end howMany
?>
drawmack is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 07:54 AM.








Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.