Topic: Count occurrences of target in array -- how?!

This one is driving me nuts. I have tried quite a few things -- all of them silly, it seems -- and googled umpteen dozen false trails. Here's the problem:

What I want to do is count the number of times a given string occurs in an array. I have the string as a variable ($myString), and the array could have n occurrences of it. (The string will always occur at least once.)

It seems so simple! I can't get it with "count", or "array_count_values", or intersections, or anything else I have tried! sad

If anyone can help me out with this, I would be most grateful!

Last edited by DjR (2008-11-29 14:18:07)

David Reimer  //  ↪ "If you know the resurrection is coming, it's impossible to be in utter darkness." — Tim Keller ↩

Re: Count occurrences of target in array -- how?!

You don't want to just loop through the array and do a preg_match or strpos and add to a counter? It may not be as elegant as one single function but it'll do the job.

$counter = 0;
for($i=0; $count = count($array); $i<=$count; $i++):
     if($string == $array[$i]) $counter++;
endfor;

Re: Count occurrences of target in array -- how?!

array.collect{ |element| element.value == "target string"}.size

Oops, that was Ruby. sorry wink

Ryan Heneise  |  Art of Mission  |  Now with extra-strong Donor Tools mojo

Re: Count occurrences of target in array -- how?!

$i = 0;
foreach($arr as $val) {
    $string == $val ? $i++ : null;
}
echo $i;

and to address this:

ryenski wrote:

array.collect{ |element| element.value == "target string"}.size

Oops, that was Ruby. sorry wink

we can write shorter than that in PHP

foreach($arr as $val) {$string == $val ? $coutner++ : null;}

i'm surprised you Ruby boys have to actually call element.value when dealing with arrays wink however it's nice to just use the .size but i assume somewhere in the language framework it actually does iterate through the array.

Last edited by tank (2008-11-29 21:31:19)

I dream with an XML intereface

Re: Count occurrences of target in array -- how?!

Pssh... that's nothing. I once wrote a line of code so short it... oh nevermind.

a.collect{|e| e.to_s == t}.size

Ryan Heneise  |  Art of Mission  |  Now with extra-strong Donor Tools mojo

Re: Count occurrences of target in array -- how?!

seriously? you post in the PHP forum giving a non-php example and you're telling me that I'm the one trying to 1-up?

my point was not that it was shorter than what you provided. Anyone can shorten variables to one character variable names. That was why i made the comment about having to write out element.value if you were posting a "shorter" example. The point was that it could be re-written as a 1 line expression... And after all this... I still have no idea why you posted your example in this forum other than to stir some php/rails debate. And really... i figured we were past all that around here.

Last edited by tank (2008-11-30 15:01:43)

I dream with an XML intereface

Re: Count occurrences of target in array -- how?!

it was a joke. in poor taste. sorry.

Ryan Heneise  |  Art of Mission  |  Now with extra-strong Donor Tools mojo

Re: Count occurrences of target in array -- how?!

Anyway, the array_map function might be PHP's equivalent of Ruby's #collect.

http://www.w3schools.com/PHP/func_array_map.asp
http://www.php.net/array_map

Ryan Heneise  |  Art of Mission  |  Now with extra-strong Donor Tools mojo

Re: Count occurrences of target in array -- how?!

Thanks to all! It will be Tuesday/Wednesday now before I can get back to this, but I have learned loads -- even more than I bargained for! wink  Much appreciated.

I'll come back to let you know how I got on when I've applied my new-found knowledge!

David Reimer  //  ↪ "If you know the resurrection is coming, it's impossible to be in utter darkness." — Tim Keller ↩

Re: Count occurrences of target in array -- how?!

ryenski wrote:

it was a joke. in poor taste. sorry.

no worries wink I was probably ribbing you a bit too much back too smile apologies to you as well.

I dream with an XML intereface

11

Re: Count occurrences of target in array -- how?!

Update: this was brilliant -- works a treat, as you knew it would! I ended up using tank's version from post #4. But since I also needed to create a new array as a selection of values from a much larger assoc. array, the bits about array_map also pointed me in a useful direction, although I used array_push in the end (I had to alter the values as they were written to the array).

My code may have ended up a bit clunky, but many thanks for the help here. MUCH appreciated!

Last edited by DjR (2008-12-02 09:06:52)

David Reimer  //  ↪ "If you know the resurrection is coming, it's impossible to be in utter darkness." — Tim Keller ↩